回顾一下开发流程:配置url, 编写视图函数,编写对应模板

  1. 配置URL
    • 首页视图匹配的 URL 去掉域名后,是一个空的字符串。每篇文章的详情有着不同的 URL,因此可以设计文章详情页面URl:<网站域名>/post/文章ID/ 时.下面依照这个规则来绑定 URL 和视图:
      # coding=utf8
      
      
      from django.conf.urls import url
      
      from .import views
      
      app_name = 'blog' urlpatterns
      = [ url(r"^$", views.index, name="index"), url(r'^post/(?P<pk>[0-9]+/$)', views.detail, name='detal'), ]

       当用户访问 post/255/ 时, (?P<pk>[0-9]+) 会匹配255,并且传递关键字参数pk=255,给视图函数即:detail(request, pk=255)?P<pk> 是 python 正则表达式的一个占位格式,表示其后匹配的内容将被存入键为 pk 的字典中。

    • 还添加了app_name = 'blog',告诉 Django 这个 urls.py 模块是属于 blog 应用的,这种技术叫做视图函数命名空间。区分不同应用中同名的视图函数
    • 给detail的url命名为detail
  • 编写视图函数
    • # coding=utf8
      
      
      from django.shortcuts import render, get_object_or_404
      from .models import Post
      
      
      # Create your views here.
      def index(request):
          post_list = Post.objects.all().order_by('-create_time')
          return render(request, 'blog/index.html', context={'post_list': post_list})
      
      
      def detail(request):
          # get_object_or_404当传入的pk对应的post数据存在时,就会返回post数据,否则返回404
          post = get_object_or_404(Post, pk=pk)
          return render(request, 'blog/detail.html', context={'post': post})

       

  • 编写blog/detail.html
    • 先修改Index.html,让点击标题和继续阅读可以进入详情页面,即给标题和继续阅读添加一个超链接
                  {% for post in post_list %}
                      <article class="post post-{{ post.pk }}">
                          <header class="entry-header">
                              <h1 class="entry-title">
                                  <!-- <a href="single.html">{{ post.title }}</a> -->
                                  <a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
                              </h1>
                              <div class="entry-meta">
                                  <span class="post-category"><a href="#">{{ post.category.name }}</a></span>
                                  <span class="post-date"><a href="#"><time class="entry-date"
                                                                            datetime="{{ post.create_time }}">2017年5月11日</time></a></span>
                                  <span class="post-author"><a href="#">{{ post.author }}</a></span>
                                  <span class="comments-link"><a href="#">4 评论</a></span>
                                  <span class="views-count"><a href="#">588 阅读</a></span>
                              </div>
                          </header>
                          <div class="entry-content clearfix">
                              <p>{{ post.excerpt }}</p>
                              <div class="read-more cl-effect-14">
                                  <!-- <a href="#" class="more-link">继续阅读 <span class="meta-nav">→</span></a> -->
                                  <a href="{{ post.get_absolute_url }}" class="more-link">继续阅读 <span class="meta-nav"></span></a>
                              </div>
                          </div>
                      </article>
                      {% empty %}
                          <div class="no-post">暂时还没有发布的文章!</div>
                      {% endfor %}
      View Code
  • 相关文章:

    • 2022-12-23
    • 2022-02-01
    • 2021-07-30
    • 2022-02-01
    • 2021-12-23
    • 2022-12-23
    • 2022-01-17
    猜你喜欢
    • 2022-12-23
    • 2021-12-07
    • 2022-12-23
    • 2022-02-14
    • 2021-08-06
    • 2021-07-13
    • 2022-12-23
    相关资源
    相似解决方案