Django TypeError修复:'NoneType' object is not iterable(特惠功能异常)
特惠功能访问报错修复方案
问题背景
站点分类功能可正常运行,但访问特惠URL时出现报错。需求为:每个商品可关联分类和/或特惠,支持跳转至特定特惠分类。
相关链接代码:
<a href="{% url 'products' %}?category=acoustic-drumkits" class="dropdown-item">Drumkits</a> <a href="{% url 'products' %}?special_offers=clearance" class="dropdown-item ">B Stock</a>
错误根源
- views.py拼写错误:处理特惠参数时将
special_offers误写为speical_offers,导致special_offers变量值为None,执行products.filter(special_offers__name__in=special_offers)时触发TypeError。 - HTML拼写错误:特惠链接的查询参数同样存在上述拼写错误。
修复步骤
- 修正views.py参数获取逻辑:
找到获取特惠参数的代码行,将request.GET.get('speical_offers')修改为request.GET.get('special_offers'),确保变量能正确获取URL中的参数值。 - 修正HTML链接参数:
检查所有特惠跳转链接,确认查询参数名为special_offers,避免拼写错误。 - 验证修复效果:
重启服务后访问特惠URL,确认商品过滤逻辑正常执行,不再触发报错。
内容的提问来源于stack exchange,提问作者Zursen
相关产品推荐
相关产品推荐

