上一章我们实现了文章详情页面。为了让文章正文能够进行标题、加粗、引用、代码块等不同的排版(像在Office中那样!),我们将使用Markdown语法。安装MarkdownMarkdown是一种轻量级的标记语言,它允许人们“使用易读易写的纯文本格式编写文档,然后转换成有效的或者HTML文档。建议读者一定要花五分钟时间熟悉一下Markdown的语法,熟练后码字效率一定会大幅提高。关于Markdo...
有了文章列表页面后,当然还需要详情页面,方便用户对某一篇感兴趣的文章深入阅读。编写视图函数打开article/views.py,增加文章详情页面的视图函数article_detail():article/views.py
...
# 文章详情
def article_detail(request, id):
# 取出相应的文章
article = ArticlePost....
注意:学本章之前请检查 Django 版本,确保安装的是 Django 2 而不是 Django 3,否则后面所有的章节都会遇到 staticfiles 无法载入的错误。在虚拟环境中输入 pip list 即可查看。上一章我们的网站页面实在太粗糙,你肯定不会拿来做真正的博客首页。因此这章我们要借助Bootstrap的力量,改写一个大气的博客。配置Bootstrap 4Bootstrap是用于...
改写视图函数上一章我们感受了视图的工作流程。为了让视图真正发挥作用,改写article/views.py中的article_list视图函数:article/views.py
from django.shortcuts import render
# 导入数据模型ArticlePost
from .models import ArticlePost
def article_list(r...
数据库虽然已经有了,但是用户通常只需要这个庞大数据库中的很小一部分进行查看、修改等操作。为此还需要代码来恰当的取出并展示数据,这一部分代码就被称为视图。Django 中视图的概念是「一类具有相同功能和模板的网页的集合」。比如,在一个博客应用中,你可能会创建如下几个视图:博客首页:展示最近的几项内容。内容“详情”页:详细展示某项内容。评论处理器:用于响应为一项内容添加评论的操作。这些需求都靠视...