django 之个人主页框架实现
标签:
JavaScript
匹配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"> <h3>文章分类</h3> <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"> <h3>标签分类</h3> <ul> {% for tag in tag_list %} <li><a href="{{ tag.tag_id }}">{{ tag.tag__title }}({{ tag.ct }})</a></li> {% endfor %} </ul></div><div> <h3>时间标签</h3> <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>
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦