使用基于类的视图

  • views.py
from django.views.generic import ListView

class PostListView(ListView):
    queryset = Post.published.all()
    context_object_name = 'posts'
    paginate_by = 1
    template_name = 'blog/post/list.html'
  • urls.py
from .views import (
	...
    PostListView,
)


urlpatterns = [
	...
    url(r'^$', PostListView.as_view(), name='post_list'),
]
  • list.html
{% for post in posts %}

{{ post.get_absolute_url }}
{{ post.title }}
{{ post.author.username }}
{{ post.publish }}
{{ post.content }}

{% endfor %}



{% include 'pagination.html' with page=page_obj %}
posted @ 2018-09-15 21:57  ret  阅读(79)  评论(0)    收藏  举报