前面我们已经实现了用Markdown语法写文章了。但是文章的评论用Markdown就不太合适了,你不能强求用户也花时间去熟悉语法啊。另外评论中通常还有表情、带颜色的字体等功能,这些也是Markdown不具备的。因此富文本编辑器Django-ckeditor就派上用场了。在后台使用Ckeditor在虚拟环境中安装django-ckeditor:(env) > pip install dj...
现在虽然博客的功能大都实现了,但是界面还是比较朴素,特别是首页的文章列表几乎全是文字,看多了难免疲劳。因此,给每个文章标题配一张标题图,不仅美观,用户也能通过图片快速了解文章内容。实际上大部分社交网站也都是这么干的,毕竟人的天性就是懒,能看图就坚决不看字。在上传用户头像章节中,我们已经接触过上传、展示图片了。标题图的实现也差不多,不同的是本章会更近一步,对图片进行缩放等处理,使页面整洁美观、...
“标签”是作者从文章中提取的核心词汇,其他用户可以通过标签快速了解文章的关注点。每一篇文章的标签可能都不一样,并且还可能拥有多个标签,这是与栏目功能不同的。好在标签功能也有优秀的三方库:Django-taggit,省得自己动手设计了。快速开发就是这样,能“借用”就不要自己重复劳动。安装及设置首先在虚拟环境中安装Django-taggit:pip install django-taggit安装...
博客的文章类型通常不止一种:有时候你会写高深莫测的技术文章,有时候又纯粹只记录一下当天的心情。因此对文章的分类就显得相当的重要了,既方便博主对文章进行分类归档,也方便用户有针对性的阅读。而文章分类一个重要的途径就是设置栏目。栏目的模型实现文章栏目功能的方法有多种。你可以只是简单的在文章的Model中增加CharField()字段,以字符串的形式将栏目名称保存起来(实际上这种实现更像是“标签”...
本章不会为博客项目增加新功能,但是也同样重要,因为我们要学习高逼格的基于类的视图。什么是类视图前面章节中写的所有视图都是基于函数的,即def;而类视图是基于类的,即class。有编程基础的同学都知道,类是面向对象技术中非常重要的概念。具有复杂数据、功能的类,可以通过继承轻而易举的将自身特性传递给另一个类,从而实现代码的高效复用。相比以前的函数视图,类视图有以下优势:HTTP方法(GET,PO...