python - List doesn't load in django templates -


so i'm trying filter model in way in views.py:

news_list = list(models.entry.objects.filter(category='news')) 

and problem list cant accessable django templates. way i'm trying access in home.html:

{% news in news_list %}  {{ news.title }}  {% endfor %} 

and this:

{{ news_list.0.title }} 

i'm sure way created list right beauce when loop through list in views.py show in terminal.

i'm using python3.3 , django 1.8.3

views.py :

#!/usr/bin/env python # -*- coding: utf-8 -*- django.views import generic . import models blog.models import entry   class blogindex(generic.listview):     queryset = models.entry.objects.published()     template_name = "home.html"     paginate_by = 3     news_list = entry.objects.filter(category='news')     = 0     x in news_list:         = i+1         print(x.title,i) 

you trying access news_list in template when infact not present in template.

you need pass news_list in context data. override get_context_data() , pass variable.

class blogindex(generic.listview):     queryset = models.entry.objects.published()     template_name = "home.html"     paginate_by = 3      def get_context_data(self, **kwargs):         context = super(blogindex, self).get_context_data(**kwargs)         context['news_list'] = entry.objects.filter(category='news') # pass 'news_list' in context         return context 

then in template, can use normal for loop.

{% news in news_list %}      {{ news.title }}  {% endfor %}  

note: don't need convert queryset list iterable.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -