Passing Django Queryset in Views to Template

左心房为你撑大大i 提交于 2019-12-10 23:28:01

问题


I have a Django Views which has some logic for passing the correct category to the template.

class ProductListView(ListView):
    model = models.Product
    template_name = "catalogue/catalogue.html"

    def get_queryset(self):
        category = self.kwargs.get("category")
        if category:
            queryset = Product.objects.filter(category__iexact=category)
        else:
            queryset = Product.objects.all()
        return queryset

I can't work out how to pass this to the template, my template code is as below:

            {% for product in products %}
            <tr>   
                <td><h5>{{ product.name }}</h5>
                <p>Cooked with chicken and mutton cumin spices</p></td>
                <td><p><strong>£ {{ product.price }}</strong></p></td>
                <td class="options"><a href="#0"><i class="icon_plus_alt2"></i></a></td>
            </tr>
            {% endfor %}  

I am pretty sure my template syntax is wrong, but how do I pass the particular category to the template? So if I have a category called 'Mains' how do I pass all the products for mains to the template.


回答1:


You can add the following method

def get_context_data(self, **kwargs):
    context = super(ProductListView, self).get_context_data(**kwargs)
    some_data = Product.objects.all()
    context.update({'some_data': some_data})
    return context

So now, in your template, you have access to some_data variable. You can also add as many data updating the context dictionary as you want.

If you still want to use the get_queryset method, then you can access that queryset in the template as object_list

{% for product in object_list %}
...
{% endfor %}



回答2:


Items from queryset in ListView are available as object_list in the template, so you need to do something like:

{% for product in object_list %}
            <tr>   
                <td><h5>{{ product.name }}</h5>
                <p>Cooked with chicken and mutton cumin spices</p></td>
                <td><p><strong>£ {{ product.price }}</strong></p></td>
                <td class="options"><a href="#0"><i class="icon_plus_alt2"></i></a></td>
            </tr>
            {% endfor %}

You can find details in the ListView documentation. Note a method called get_context_data - it returns a dictionary of variables and values, that will be passed to templates. You can always find why it works in this way in the source code.




回答3:


A more elegant way to pass data to the template context is to use the built-in view variable. So rather than overriding the get_context_data() method, you can simply create a custom method that returns a queryset:

def stores(self):
    return Store.objects.all()

Then you can use this in the template:

{% for store in view.stores %}
  ...
{% endfor %}

See also:

  • https://reinout.vanrees.org/weblog/2014/05/19/context.html
  • https://docs.djangoproject.com/en/dev/releases/1.5/#new-view-variable-in-class-based-views-context


来源:https://stackoverflow.com/questions/48544128/passing-django-queryset-in-views-to-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!