P
P
Peterson_s2020-10-17 01:05:25
Django
Peterson_s, 2020-10-17 01:05:25

How to send information from queryset to template?

I need to return a list of books by the same author. I am using the get method to get information from the db.
views.py

class AuthorDetailView(generic.DetailView):
    
    model = Author
    
    template_name = 'catalog/author_detail.html'
    def get(self, request, pk):
        queryset = Book.objects.all().filter(author=pk).values('title', 'summary')
        
        
        return render(request, self.template_name, context={'queryset':queryset})

I get this response:
<QuerySet [{'title': 'Programing c++', 'summary': 'about c++'}]>


How to kill the queryset or how to get information from the database using this AuthorDetailView class in order to use this data in the template later.
Ps. I am just learning

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SKY_nv, 2020-10-17
@Peterson_s

{% for book in queryset %}
<p>Название: {{ book.title }}</p>
<p>Описание: {{ book.summary }}</p>
{% endfor %}

In general, you need to open the documentation on the chapter TEMPLATES.

D
dooMoob, 2020-10-17
@dooMoob

Iterate over qs in template

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question