I have a set of questions in the database. I want to display each question in a collapsible item as a list. I used to use TemplateView:
class questionmanager(TemplateView): template_name = 'questionmanager.html' questions = Question.objects.all() def get_context_data(self, **kwargs): context = ({ 'questions': self.questions, }) return context
Then I read that using ListView is best practice for presenting a list of objects. Then I changed my class to this:
class QuestionListView(ListView): model = Question def get_context_data(self, **kwargs): context = super(QuestionListView, self).get_context_data(**kwargs) return context
In the old template, I used this for a loop:
{% for question in questions %}
I thought I didn't need to use a for loop when I use ListView instead of TemplateView; but I could not list the elements without a for loop. I found an example here , and it seems to me that the difference is that in the for loop we use object_list ( {% for question in **object_list** %} ) instead of using the argument we pass in context.
I really do not see such a big difference between using TemplateView and ListView - spending an hour on this. I would appreciate someone explaining why using ListView instead of TemplateView is best practice (in this case).
Thanks in advance.
melis source share