(三)Django快速搭建简易个人播客(页面配置)

  {#根据标签分类#}
         

{{ article_num }} 篇文章

{% for article_index in articles_index.object_list %}

{{ article_index.title }}

作者:{{ article_index.name.nick_name }}分类:【{{ article_index.get_classify_display }}浏览({{ article_index.click_num }}

{{ article_index.add_time | center:'1'}}
{% endfor %}

在settinggs,py配置路由


在my_blog App下新建urls

# !/usr/bin/env python 
# -*- coding:utf-8 -*-
__author__ = '_X.xx_'
__date__ = '2018/6/21 22:10'

from django.conf.urls import url
from .views import AboutView, ArticleView, AddCommentsView, ProjectView, \
    ProjectDetailView, TemplateView

urlpatterns = [
    # 关于我页面
    url(r'^about$', AboutView.as_view(), name='about'),
    # 图片显示页
    url(r'^template$', TemplateView.as_view(), name='template'),
    # 点击阅读全文跳转页面
    url(r'^detail/(?P\d+)/$', ArticleView.as_view(), name='detail'),
    # 添加文章评论
    url(r'^add_comment/(?P\d+)$', AddCommentsView.as_view(),
        name="add_comment"),
    # 项目简介
    url(r'^project$', ProjectView.as_view(), name='project'),
    # 点击阅读全文跳转页面
    url(r'^project_detail/(?P\d+)/$', ProjectDetailView.as_view(),
        name='project_detail'),

]
编写视图函数

class IndexView(View):
    """
        首页文章
    """

    def get(self, request):
        # 取出首页文章
        articles_index = IndexArticle.objects.all()
        # 根据添加时间排序
        article_news = IndexArticle.objects.all().order_by('add_time')[:10]
        # 根据点击量排序
        article_clicks = IndexArticle.objects.all().order_by('-click_num')[:10]
        # 分类显示文章
        bj_articles = request.GET.get('ct', '')
        if bj_articles:
            articles_index = articles_index.filter(classify=bj_articles)
        # 对文章进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(articles_index, 2, request=request)
        articles = p.page(page)

        # 对文章数统计
        article_num = articles_index.count()
        return render(request, 'my_blog/index.html', {
            'articles_index': articles,
            'article_news': article_news,
            'article_clicks': article_clicks,
            'bj_articles': bj_articles,
            'article_num': article_num
        })

你可能感兴趣的:(Django)