标签:pat hid span utf-8 bsp from int 比较 list
我们需要的页面:
l 博客首页
l 博文详情页
l 分类列表页
l 标签列表页
l 友链展示页
但实际上,博客首页,分类列表页和标签列表页,本质上都是文章列表页,只有一些信息有差别.那么View大概就有三类:
l 列表页: 根据不同的查询条件分别展示博客首页,分类列表页和标签列表页
l 博文详情页: 展示博文详情页
l 友链展示页: 展示所有友情链接
书上使用的环境是django1.11.1,而我使用的是3.0.4,所以路由我使用path来写,并且把路由分散开来写,让代码分工能明确一点.
主项目中的urls.py
1 urlpatterns = [ 2 path(‘admin/‘, custom_site.urls), 3 path(‘super_admin/‘, admin.site.urls), 4 path(‘config/‘, include(‘config.urls‘), name=‘config‘), 5 re_path(r‘^‘, include(‘blogApp.urls‘), name=‘blog‘), 6 ]
blogApp中的urls.py
1 app_name = ‘blog‘ 2 urlpatterns = [ 3 path(‘category/<int:category_id>‘, views.post_list, name=‘category-list‘), 4 path(‘tag/<int:tag_id>‘, views.post_list, name=‘tag-list‘), 5 path(‘post/<int:post_id>.html‘, views.post_detail, name=‘post-detail‘), 6 re_path(r‘^‘, views.post_list, name=‘post_list‘), 7 ]
config中的urls.py
1 app_name = ‘config‘ 2 urlpatterns = [ 3 path(‘links/‘, views.links, name=‘links‘), 4 ]
注意app_name必须与主路由中的name保持一致
接着是编写views,因为前面逻辑还是比较简单的,我就直接贴代码了
1 from django.http import HttpResponse 2 from django.shortcuts import render 3 4 from blogApp.models import Tag, Post 5 6 7 def post_list(request, category_id=None, tag_id=None): 8 tag = None 9 category = None 10 if tag_id: 11 try: 12 tag = Tag.objects.get(id=tag_id) 13 except Tag.DoesNotExist: 14 post_lists = [] 15 else: 16 post_lists = tag.post_set.filter(status=Post.STATUS_NORMAL) # 标签列表 17 else: 18 post_lists = Post.objects.filter(status=Post.STATUS_NORMAL) # 首页 19 if category_id: 20 try: 21 category = Category.objects.get(id=category_id) # 分类列表 22 except Category.DoesNotExist: 23 category = None 24 else: 25 post_lists = post_lists.filter(category_id=category_id) 26 27 context = { 28 ‘category‘: category, 29 ‘tag‘: tag, 30 ‘post_lists‘: post_lists, 31 } 32 33 return render(request, ‘list.html‘, context=context) 34 35 36 def post_detail(request, post_id): 37 try: 38 post = Post.objects.get(id=post_id) 39 except Post.DoesNotExist: 40 post = None 41 42 return render(request, ‘detail.html‘, context={‘post‘: post})
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>List</title> 6 </head> 7 <body> 8 {% if tag %} 9 标签页: {{ tag.name }} 10 {% endif %} 11 12 {% if category %} 13 分类页: {{ category.name }} 14 {% endif %} 15 <ul> 16 {% for post in post_lists %} 17 <li> 18 <a href="{% url ‘blog:post-detail‘ post.id %}">{{ post.title }}</a> 19 <div> 20 <span>作者:{{ post.owner.username }}</span> 21 <span>分类:{{ post.category.name }}</span> 22 </div> 23 <p>{{ post.desc }}</p> 24 </li> 25 {% endfor %} 26 </ul> 27 </body> 28 </html>
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Detail</title> 6 </head> 7 <body> 8 {% if post %} 9 <h1>{{ post.title }}</h1> 10 <div> 11 <span>作者:{{ post.owner.username }}</span> 12 <span>分类:{{ post.category.name }}</span> 13 </div> 14 <hr> 15 <p> 16 {{ post.content }} 17 </p> 18 {% endif %} 19 </body> 20 </html>
效果:
标签:pat hid span utf-8 bsp from int 比较 list
原文地址:https://www.cnblogs.com/ylnx-tl/p/12623292.html