I have a html page where is
<body>
<h8>
MY BLOGS
</h8>
<ul>
{% for bl in all_blogs %}
<li>{{bl.header}}</li>
{% endfor %}
</ul>
In "ul" it must show all items from all_blogs what is
def list_blogs(request):
all_blogs = Blog.objects.all
return render(request, 'blogs.html', {'blogs': all_blogs})
and Blog is
class Blog(models.Model):
header = models.CharField(max_length=100)
data = models.CharField(max_length=500)
def __str__(self):
return self.header
and the output of the html page is just "MY BLOG" without any blogs in it, in my admin panel I have 4 blogs, I tried to print
for bl in all_blogs:
print(str(bl))
but it doesnt work, I also checked local db for blogs, and it was fine.
all()
[Django-doc] is a method, not a property, so you should call it to obtain the queryset of all Blog
objects, so:
def list_blogs(request):
all_blogs = Blog.objects
return render(request, 'blogs.html', {'blogs': all_blogs})
Furthermore in the context, you named the queryset blogs
, not all_blogs
(that is the name of the variable in the view, but you pass it as 'blogs'
to the context), hence you should either change the key in the view to 'all_blogs'
, or change the variable in the template to:
<ul>
{% for bl in %}
<li>{{bl.header}}</li>
{% endfor %}
</ul>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.