【问题标题】:Setting GET parameters in url in Django在 Django 的 url 中设置 GET 参数
【发布时间】:2015-11-18 08:53:06
【问题描述】:

我想创建一个带有各种可选过滤器的列表视图。我的网址应该是这样的:

shop/category_6/?manufacturer_id=3

当我将此 url 放入浏览器表单时效果很好。但是当我尝试将此选项添加到模板时,它给了我 NoReverseMatch。我的模板:

{% for manufacturer in manufacturer_list %}
    <p><a href = "{% url 'shop:item_list' category_id=category.id manufacturer_id=manufacturer.0 %}">{{manufacturer.1}}</a></p>
{% endfor %}

这是我的看法:

class ItemListView(ListView):
    model = Item
    context_object_name = 'item_list'
    template_name = 'shop/item_list.html'

def get_queryset(self, **kwargs):
    full_item_list=Item.objects.all()
    queryset=full_item_list.filter(category__id=int(self.kwargs['category_id']))
    manufacturer_id = self.request.GET.get('manufacturer_id')
    if manufacturer_id:
        queryset=queryset.filter(manufacturer__id=int(manufacturer_id))
    return queryset

def get_context_data(self, **kwargs):
    context = super(ItemListView, self).get_context_data(**kwargs)
    context['category']=get_object_or_404(Category, pk=self.kwargs['category_id'])
    context['manufacturer_list'] = self.get_queryset(**kwargs).values_list('manufacturer__id', 'manufacturer__name').distinct().order_by('manufacturer__name')
    return context

我的错误是什么?

【问题讨论】:

    标签: python django url django-templates django-views


    【解决方案1】:

    您没有配置 GET 参数...可能是 shop/category_6/?manufacturer_id=3

    <a href = "{% url 'shop:item_list' category_id=category.id %}?manufacturer_id={{ manufacturer.0 }}">{{manufacturer.1 }}</a>
    

    【讨论】:

      猜你喜欢
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 2016-08-13
      • 2020-03-26
      • 2021-03-25
      • 1970-01-01
      相关资源
      最近更新 更多