技术交流QQ群:1027579432,欢迎你的加入!

本教程来源于B站杨仕航Django2.0开发视频教程,如需转载,必须注明来源!

1.继续搭建blog

继续搭建blog.png
  • 打开blog目录下的views.py文件,添加如下内容,实现通过url向服务器发送请求,服务器收到请求后进行响应,把结果显示到前端页面的功能。
    from django.shortcuts import render_to_response, get_object_or_404
    from .models import Blog
    # Create your views here.
    
    
    def blog_list(request):
        context = {}
        context['blogs'] = Blog.objects.all()
        return render_to_response('blog_list.html', context)
    
    
    def blog_detail(request, blog_pk):
        context = {}
        context['blog'] = get_object_or_404(Blog, pk=blog_pk)
        return render_to_response('blog_detail.html', context)
  • 创建模板页面:打开blog目录,在目录下面创建templates文件夹,在该文件夹下创建blog_list.html和blog_detail.html两个文件(ps:按下英文状态的!后,再按下Tab键,可以快速创建HTML5代码骨架),文件具体内容如下所示:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>我的网站</title>
    </head>
    <body>
    <!-- blog_list.html -->
    <!-- 下一行中的blogs来自于views.py中的context['blogs'] = Blog.objects.all() -->
        {% for blog in blogs %}
        <!-- 下一行中的blog.title来自于models.py中的title = models.CharField(max_length=50) -->
            <h3>{{ blog.title }}</h3>
            <p>{{ blog.content }}</p>
        {% endfor %}
    </body>
    </html>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{{ blog.title }}</title>
    </head>
    <body>
        <!-- blog_detail.html -->
        <h3>{{ blog.title }}</h3>
        <p>{{ blog.content }}</p>
    </body>
    </html>
创建模板页面.png
  • 创建路由设置文件:打开blog目录,在目录下面创建urls.py文件,具体文件内容如下所示:
    from django.urls import path
    from . import views
    
    
    urlpatterns = [
        # http:localhost:8000/blog/1
        path('<int:blog_pk>', views.blog_detail, name="blog_detail"),
    ]
  • 打开mysite目录下的全局路由设置文件urls.py,在当中添加如下内容:
    from django.contrib import admin
    from django.urls import include, path
    from blog.views import blog_list
    
    urlpatterns = [
        # 博客首页
        path('', blog_list, name="home"),
        path('admin/', admin.site.urls),
        path('blog/', include('blog.urls')),
    
    ]
  • 打开浏览器,在当中输入网站localhost:8000打开首页,显示如下页面:


    首页.png
  • 对上述首页进行前端页面优化,打开blog_list.html文件,添加如下内容后的文件内容:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>我的网站</title>
    </head>
    <body>
    <!-- blog_list.html -->
        <div>
            <a href="{% url 'home' %}">
                <h3>个人博客网站</h3>
            </a>
        </div>
        <hr>
    <!-- 下一行中的blogs来自于views.py中的context['blogs'] = Blog.objects.all() -->
        {% for blog in blogs %}
        <!-- 下一行中的blog.title来自于models.py中的title = models.CharField(max_length=50) -->
            <a href="{% url 'blog_detail' blog.pk %}"><h3>{{ blog.title }}</h3></a>
            <p>{{ blog.content }}</p>
        {% endfor %}
    </body>
    </html>
  • 对博客详情页面进行前端页面优化,打开blog_detail.html文件,添加如下内容后的文件内容:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{{ blog.title }}</title>
    </head>
    <body>
        <!-- blog_detail.html -->
        <div>
            <a href="{% url 'home' %}">
                <h3>个人博客网站</h3>
            </a>
        </div>
        <h3>{{ blog.title }}</h3>
        <p>{{ blog.content }}</p>
    </body>
    </html>
  • 对首页进行前端页面优化从而显示总共的博客数量,有两种方法可以实现。推荐方法1:打开blog_list.html文件,添加如下内容后的文件内容:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>我的网站</title>
    </head>
    <body>
    <!-- blog_list.html -->
        <div>
            <a href="{% url 'home' %}">
                <h3>个人博客网站</h3>
            </a>
        </div>
        <hr>
        <p>一共有{{ blogs|length }}篇博客</p>
        <!-- 方法2:{{ blogs_count }} -->
    <!-- 下一行中的blogs来自于views.py中的context['blogs'] = Blog.objects.all() -->
        {% for blog in blogs %}
        <!-- 下一行中的blog.title来自于models.py中的title = models.CharField(max_length=50) -->
            <a href="{% url 'blog_detail' blog.pk %}"><h3>{{ blog.title }}</h3></a>
            <p>{{ blog.content }}</p>
        {% endfor %}
    </body>
    </html>
  • 对首页进行前端页面优化从而显示总共的博客数量,有两种方法可以实现。方法2:打开blog目录下的views.py文件,添加如下内容后的文件内容:
    from django.shortcuts import render_to_response, get_object_or_404
    from .models import Blog
    # Create your views here.
    
    
    def blog_list(request):
        context = {}
        context['blogs'] = Blog.objects.all()
        context['blogs_count'] = Blog.objects.all().count()  # 统计总共的博客数量
        return render_to_response('blog_list.html', context)
    
    
    def blog_detail(request, blog_pk):
        context = {}
        context['blog'] = get_object_or_404(Blog, pk=blog_pk)
        return render_to_response('blog_detail.html', context)
  • 当全部删除完所有博客后,对首页进行前端页面优化,给出相应的提示。打开blog_list.html文件,添加如下内容后的文件内容:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>我的网站</title>
    </head>
    <body>
    <!-- blog_list.html -->
        <div>
            <a href="{% url 'home' %}">
                <h3>个人博客网站</h3>
            </a>
        </div>
        <hr>
    <!-- 下一行中的blogs来自于views.py中的context['blogs'] = Blog.objects.all() -->
        {% for blog in blogs %}
        <!-- 下一行中的blog.title来自于models.py中的title = models.CharField(max_length=50) -->
            <a href="{% url 'blog_detail' blog.pk %}"><h3>{{ blog.title }}</h3></a>
            <p>{{ blog.content }}</p>
        {% empty %}
            <p>--暂无博客,敬请期待--</p>
        {% endfor %}
        <p>一共有{{ blogs|length }}篇博客</p>
    </body>
    </html>
  • 当有长内容的博客内容时,对首页进行前端页面优化,只显示前面几行的内容。打开blog_list.html文件,添加如下内容后的文件内容:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>我的网站</title>
    </head>
    <body>
    <!-- blog_list.html -->
        <div>
            <a href="{% url 'home' %}">
                <h3>个人博客网站</h3>
            </a>
        </div>
        <hr>
    <!-- 下一行中的blogs来自于views.py中的context['blogs'] = Blog.objects.all() -->
        {% for blog in blogs %}
        <!-- 下一行中的blog.title来自于models.py中的title = models.CharField(max_length=50) -->
            <a href="{% url 'blog_detail' blog.pk %}"><h3>{{ blog.title }}</h3></a>
            <p>{{ blog.content|truncatechars:30 }}</p>  # |表示过滤器,{{ blog.content|truncatechars:30 }}表示只显示博客内容的前30个字
        {% empty %}
            <p>--暂无博客,敬请期待--</p>
        {% endfor %}
        <p>一共有{{ blogs|length }}篇博客</p>
    </body>
    </html>
  • 打开博客详情页面内容文件blog_detail.html,添加作者信息等内容后的文件内容:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{{ blog.title }}</title>
    </head>
    <body>
        <!-- blog_detail.html -->
        <div>
            <a href="{% url 'home' %}">
                <h3>个人博客网站</h3>
            </a>
        </div>
        <h3>{{ blog.title }}</h3>
        <p>作者: {{ blog.author }}</p>
        <p>发表日期: {{ blog.created_time|date:"Y-m-d G:i:s"}}</p>
        <p>分类: {{ blog.blog_type }}</p>
        <p>{{ blog.content }}</p>
    </body>
    </html>
  • 关于网页显示时间与中国时间相差8小时的处理方法见该链接https://www.cnblogs.com/fixdq/p/9887575.html
  • 需求:例如点击博客详情页面中显示的分类:随笔,可以将属于随笔类别的博客全部显示出来。首先打开blog文件下的urls.py文件,添加如下内容:
    from django.urls import path
    from . import views
    
    
    urlpatterns = [
        # http:localhost:8000/blog/1
        path('<int:blog_pk>', views.blog_detail, name="blog_detail"),
        path('<int:blog_type_pk>', views.blogs_with_type, name="blogs_with_type"),
    ]
  • 接着打开blog文件下的views.py文件,添加blogs_with_type处理方法如下所示:
    from django.shortcuts import render_to_response, get_object_or_404
    from .models import Blog, BlogType
    # Create your views here.
    
    
    def blog_list(request):
        context = {}
        context['blogs'] = Blog.objects.all()
        return render_to_response('blog_list.html', context)
    
    
    def blog_detail(request, blog_pk):
        context = {}
        context['blog'] = get_object_or_404(Blog, pk=blog_pk)
        return render_to_response('blog_detail.html', context)
    
    
    def blogs_with_type(request, blogs_with_type):
        context = {}
        blog_type = get_object_or_404(BlogType, pk=blogs_with_type)
        context['blogs'] = Blog.objects.filter(blog_type=blog_type)
        context['blog_type'] = blog_type
        return render_to_response('blogs_with_type.html', context)
  • 然后根据上一步中的views.py文件可知,还需要创建模板页面。因此,打开blog文件夹下的templates文件目录,增加blogs_with_type.html文件。如下所示:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{{ blog_type.type_name }}</title>
    </head>
    <body>
    <!-- blog_list.html -->
        <div>
            <a href="{% url 'home' %}">
                <h3>个人博客网站</h3>
            </a>
        </div>
        <hr>
        <h3>{{ blog_type.type_name }}</h3>
    <!-- 下一行中的blogs来自于views.py中的context['blogs'] = Blog.objects.all() -->
        {% for blog in blogs %}
        <!-- 下一行中的blog.title来自于models.py中的title = models.CharField(max_length=50) -->
            <a href="{% url 'blog_detail' blog.pk %}"><h3>{{ blog.title }}</h3></a>
            <p>{{ blog.content|truncatechars:30 }}</p>
        {% empty %}
            <p>--暂无博客,敬请期待--</p>
        {% endfor %}
        <p>一共有{{ blogs|length }}篇博客</p>
    </body>
    </html>
  • 同时,对blog_detail.html文件进行修改,修改部分如下:
    <p>分类: {{ blog.blog_type }}</p>
    ---------------------- 修改为 --------------------------
    <p>分类:
        <a href="{% url 'blogs_with_type' blog.blog_type.pk%} ">
            {{ blog.blog_type }}
        </a>
    </p>
  • 点击博客详情页面分类:随笔,会出现如下错误。原因是由于blog文件目录下的urls.py文件中的路由设置有问题,因此对该文件下的内容进行修改,如下所示:
    错误.png
    from django.urls import path
    from . import views
    
    
    urlpatterns = [
        # http:localhost:8000/blog/1
        path('<int:blog_pk>', views.blog_detail, name="blog_detail"),
        path('type/<int:blog_type_pk>', views.blogs_with_type, name="blogs_with_type"),  # 增加一个type参数与上面一个路由进行区分
    ]
  • 点击博客详情页面分类:随笔,会继续出现上面的错误。原因是由于blog文件目录下的views.py文件中blogs_with_type()方法所传入的参数blogs_with_type名称不对,应该传入的参数名称为blog_type_pk。因此对该文件下的内容进行修改,如下所示:
    def blogs_with_type(request, blog_type_pk):
        context = {}
        blog_type = get_object_or_404(BlogType, pk=blog_type_pk)
        context['blogs'] = Blog.objects.filter(blog_type=blog_type)
        context['blog_type'] = blog_type
        return render_to_response('blogs_with_type.html', context)

2.常用的模板标签

常用的模板标签.png

3.常用的过滤器

常用的过滤器.png

3.本节结果展示

结果1.png

结果2.png

结果3.png