简体   繁体   English

无法使用Django使用CBV渲染模型

[英]Can't render model using CBV using django

I am currently learning CBV and using it over function view. 我目前正在学习CBV,并在功能视图上使用它。 However, I have difficulty rendering it. 但是,我很难渲染它。

My template Post_list.html 我的模板Post_list.html

{% for post in post_list %}
    <p class="article-content">{{ object.content }}</p>
{% endfor %}

and my view 和我的看法

class PostListView(ListView):
    model = Post

I want to render it into a loop like I used to do with function view. 我想像以前使用函数视图那样将其渲染成一个循环。

Do the following in your view. 在您的视图中执行以下操作。

class PostListView(ListView):
    model = Post
    context_object_name = 'post_list'

If you don't specify a context_object_name it will be object_list . 如果您未指定context_object_name ,它将为object_list

Also, in your template: 另外,在您的模板中:

{% for post in post_list %}
    <p class="article-content">{{ post.content }}</p>
{% endfor %}

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

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