简体   繁体   English

如何从django-admin面板获取所有项目?

[英]How to get all items from django-admin panel?

I have a html page where is 我有一个html页面在哪里

<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 在“ ul”中,它必须显示all_blogs中的所有项目是什么

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 并且html页面的输出仅为“ MY BLOG”,其中没有任何博客,在管理面板中我有4个博客,我尝试打印

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: all() [Django-doc]是一个方法,而不是属性,因此应调用它以获取所有Blog对象的queryset,因此:

def list_blogs(request):
    all_blogs = Blog.objects.all()
    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: 此外,在上下文中,您将queryset命名为blogs而不是 all_blogs (这是视图中变量的名称,但是将其作为'blogs'传递给上下文),因此您应该将视图中的键更改为'all_blogs' ,或将模板中的变量更改为:

<ul>
    {% for bl in blogs %}
        <li>{{bl.header}}</li>
    {% endfor %}
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM