上一节已经学习了,如何给时间,标签,分类,来分类。这一节学习下如何把这些内容写到页面上去
匹配url

url(r'^(\w+)/$', views.home),

创建home函数

from django.db.models import Countdef home(request,site): blog = models.Blog.objects.filter(site=site).first() if not blog: return redirect('/') category_list = models.Article.objects.filter(blog=blog).values('category_id','category__title').annotate(ct=Count('nid')) tag_list = models.Article2Tag.objects.filter(article__blog=blog).values('tag_id','tag__title').annotate(ct=Count('id')) date_list = models.Article.objects.filter(blog=blog).extra(select={'ctime':"strftime('%%Y-%%m',create_time)"}).values('ctime').annotate(ct=Count('nid')) article_list = models.Article.objects.all() #return HttpResponse('......') return render( request, 'home.html', { 'blog':blog, 'category_list':category_list, 'tag_list':tag_list, 'date_list':date_list, 'article_list':article_list } )

创建home页面

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/css/theme/{{ blog.theme }}.css"></head><body><div>{{ blog.title }}--{{ blog.user.nickname }}</div><div class="c1"> <h4>文章分类</h4> <ul> {% for category in category_list %} <li><a href="{{ category.category_id }}">{{ category.category__title }}({{ category.ct }})</a></li> {% endfor %} </ul></div><div class="c2"> <h4>标签分类</h4> <ul> {% for tag in tag_list %} <li><a href="{{ tag.tag_id }}">{{ tag.tag__title }}({{ tag.ct }})</a></li> {% endfor %} </ul></div><div> <h4>时间标签</h4> <ul> {% for date in date_list %} <li><a href="{{ date.ctime }}">{{ date.ctime }}({{ date.ct }})</a></li> {% endfor %} </ul></div><div> {% for article in article_list %} <div> <a href="{{ article.title }}"></a> <div>{{ article.summary }}</div> </div> {% endfor %}</div></body></html>

预览

在主题里面通过link导入不同的css文件,给每个人设置不同的样式