💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
>[success] # 页面展示整理 ~~~ 1.根据model,构建我们搭建的,一下几个页面的展示: 博客首页 博文详情页 分类列表页 标签列表页 友链展示页 2.根据上面划分,讲view 页划分如下 列表view——根据不同的查询条件分别展示:首页,分类列表页,标签列表页 文章页view —— 展示文章页 友链页view —— 展示所有友情链接 ~~~ >[danger] ##### urls.py 创建 `在全局url创建` ~~~ from django.conf.urls import url from django.contrib import admin from blog.views import post_list, post_detail from config.views import links from .custom_site import custom_site urlpatterns = [ url(r"^$", post_list), url(r'^category/(?P<category_id>\d+)/$', post_list, name="category"), url(r'^tag/(?P<tag_id>\d+)/$', post_list), url(r'^post/(?P<post_id>\d+)/$', post_detail,name="detail"), url(r'^links/$', links), url(r'^admin/', admin.site.urls), url(r'^cus_admin/', custom_site.urls), ] ~~~ >[danger] ##### view.py `blog目录下的views.py` ~~~ from django.shortcuts import render def post_list(request, category_id=None, tag_id=None): return render(request, 'blog/list.html', context={'name': 'post_list'}) def post_detail(request, post_id=None): return render(request, 'blog/detail.html', context={'name': 'post_detail'}) ~~~ >[danger] ##### templates ~~~ list.html文件如下: <h1>list</h1> {{ name }} detail.html文件如下: <h1>detail</h1> {{ name }} ~~~ >[danger] ##### templates 报错处理 ~~~ TemplateDoesNotExist at / blog/list.html ~~~ * 根据提示分析 ![](https://box.kancloud.cn/c25d88d17a522b2637efcb765a941b1b_1057x187.png) * 处理问题所在位置 ![](https://box.kancloud.cn/e79dd21f5130c5541ded70c30eb4801c_699x250.png) * 另一种处理 ~~~ 这个时候你就要看看它有没有去查找你放模板文件的目录,如果没有,那说明你 的模板不在它的查找范围,你需要去配置APP_INSTALL,如果它列出了你防止模 板的app,但是提示未找到,那你应该检查下模板名称是不是有写错。 ~~~ >[success] # FBV 数据应用 >[danger]##### blog/views.py * 调用的包 ~~~ from django.shortcuts import render from django.http import Http404 from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger from django.db import connection from .models import Post, Tag, Categroy from config.models import SideBar from comment.models import Comment # Create your views here. ~~~ * 提取共同数据,博客系统的 侧边栏 ~~~ 1.categroy = Categroy.objects.filter(status=1) 注最好将1给予实际命名 # 从一个sql 中取出两种状态 两个方式 不推荐进行了两次sql: categroy.filter(is_nav=True) categroy.filter(is_nav=False) 2.推荐:使用下面案例写法 ~~~ ~~~ def get_common_context(): # 类别展示 categories = Categroy.objects.filter(status=1) nav_cates = [] cates = [] for cate in categories: if cate.is_nav: nav_cates.append(cate) else: cates.reverse(cate) # 边栏展示 side_bars = SideBar.objects.filter(status=1) recently_posts = Post.objects.filter(status=1)[:10] # host_posts = Post.objects.filter(status=1).order_by('views')[:10] recently_comments = Comment.objects.filter(status=1)[:10] context = { 'nav_cates':nav_cates, "cates":cates, 'side_bars':side_bars, 'recently_comments':recently_comments, 'recently_posts':recently_posts, } return context ~~~ * 首页展示 ~~~ def post_list(request, category_id=None, tag_id=None): current_page = request.GET.get("page", 1) page_size = 2 # 分页 异常处理 try: current_page =int(current_page) except: current_page = 1 queryset = Post.objects.all() if category_id: queryset = queryset.filter(category_id=category_id) # 多对多数据处理 elif tag_id: try: tag = Tag.objects.get(id=tag_id) except Tag.DoesNotExist: queryset = [] else: queryset = tag.post_set.all() # 分页 paginator = Paginator(queryset.order_by('id'), page_size) try: posts = paginator.page(current_page) except EmptyPage: posts = paginator.page(paginator.num_pages) context = { "posts":posts, } # 对共同数据处理添加 common_context = get_common_context() context.update(common_context) return render(request, 'blog/list.html', context=context) ~~~ * 内容展示 ~~~ def post_detail(request, post_id=None): queryset = Post.objects.all() if post_id: try: queryset = queryset.get(id=post_id) except Post.DoesNotExist: raise Http404("不存在") context = { "posts": queryset } common_context = get_common_context() context.update(common_context) return render(request, 'blog/detail.html', context=context) ~~~ >[danger] ##### html * base.html 模板页面 ~~~ <h1>list</h1> <ul> {% for cate in nav_cates %} <li> <a href="{% url 'category' cate.id %}">{{ cate.name }}</a> </li> {% endfor %} </ul> {% block content %} {% if posts %} {% for post in posts %} title: <a href="{% url 'detail' post.id %}">{{ post.title }}</a><br> desc:{{ post.desc }} <hr/> {% endfor %} {% if posts.has_previous %}<a href="?page={{ posts.number|add:"-1" }}">上一页</a>{% endif %} page {{ posts.number }} of {{ posts.paginator.num_pages }}. {% if posts.has_next %}<a href="?page={{ posts.number|add:"1" }}">下一页</a>{% endif %} {% else %} 没有数据 {% endif %} <hr/> {% endblock %} <ul> {% for cate in cates %} <li> <a href="{% url 'category' cate.id %}">{{ cate.name }}</a> </li> {% endfor %} </ul> // 侧边展示 <ul> {% for side in side_bars %} <h4>{{ side.title }}</h4> <div> {% if side.display_type == 1 %} {{ side.content|safe }} {% elif side.display_type == 2 %} <ul> {% for post in recently_posts %} <li>{{ post.title }}</li> {% endfor %} </ul> {% elif side.display_type == 4 %} <ul> {% for comment in recently_commnent %} <li>{{ comment.content }}</li> {% endfor %} </ul> {% endif %} </div> <div> {{ side.display_type }} </div> {% endfor %} </ul> ~~~ * 首页 ~~~ {% extends 'blog/base.html' %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> </html> ~~~ * 内容页 ~~~ {% extends "blog/base.html" %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% block content %} <h1>detail</h1> <a href="/"></a> {{ posts.content }} {% endblock %} </body> </html> ~~~