Django development blog system (08- routing configuration and written views)

We need to page:

l blog Home

l Bowen detail page

l Category List

l tag list page

l Friends of the chain show page

 

But in fact, the blog home page, category page and a list of tag list page, the article is essentially a list of pages, only some of the information makes a difference so there is probably three categories View:

l List: Depending on the query respectively show blog home page, category page and a list of tag list page

l Bowen details page: Details page shows Bowen

l Friends of the chain show page: Show All Links

 

Use the book environment is django1.11.1, and I'm using 3.0.4, so I use the routing path to write, and write the route spread out, so that the code can be clear division of labor.

The main project 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 in 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 in urls.py

1 app_name = 'config'
2 urlpatterns = [
3     path('links/', views.links, name='links'),
4 ]

 

Note app_name must be consistent with the main route of the name

 

Followed by the preparation of views, because the logic is quite simple in front, I'll just paste the code

 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})
blogApp/views

 

 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>
list.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>
detail.html

 

effect:

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/ylnx-tl/p/12623292.html