随着时间的推移(加上勤奋的写作!),你的博客文章一定会越来越多。如果不进行处理,可能同一个页面会挤上成百上千的文章,不美观不说,还降低了页面的反应速度。这个时候就需要对文章进行分页的处理。利用轮子写一个完善的分页功能是有些难度的,好在Django已经帮你准备好一个现成的分页模块了(Django把大部分基础功能都替你准备好了!)。内置模块虽然简单,但是对博客来说完全足够了。我们要用到的是Pag...
到目前为止我们的博客处理的都是文字。现代互联网早就进入了“读图”时代,图片的维护、展示也就相当重要。上一章中预留了avatar字段,用来保存用户上传的头像,现在我们来实现这个功能。必要的设置图片属于一种媒体文件,它与静态文件类似,需要设置一个统一的目录,便于集中存储和访问。这类需要框架统一设置的参数,当然应该在/my_blog/settings.py中。在底部加上:/my_blog/sett...
可能你已经发现了,Django自带的User模型非常实用,以至于我们没有写用户管理相关的任何模型。但是自带的User毕竟可用的字段较少。比方说非常重要的电话号码、头像等都没有。解决的方法有很多,你可以不使用User,自己从零写用户模型;也可以对User模型进行扩展。博客网站的用户信息并不复杂,因此扩展User就足够了。扩展User模型扩展User模型又有不同的方法。在大多数情况下,使用模型一...
随着技术的发展,验证用户身份的手段越来越多,指纹、面容、声纹应有尽有,但密码依然是最重要的手段。互联网处处都有密码的身影,甚至变成了现代人的一种负担。像笔者这样的,动辄几十个账号密码,忘记其中几个简直太正常了。本章讲如何帮助健忘症患者,重置用户密码。安装第三方库前面我们已经知道如何修改文章标题、正文等内容,但是密码作为验证身份的重要口令,必须以更加稳妥的方式修改。一种比较常用的方式是发送一封...
这一章将实现删除用户数据的功能。实际上删除用户和前面的删除文章是完全类似的,有了之前的铺垫,这一章会非常的轻松。权限与视图用户数据是很多网站最重要的财产,确保用户数据的安全是非常重要的。前面学习的用户登录、退出、创建都是相对安全的操作;而删除数据就很危险,弄不好会造成不可逆的损失。因此我们希望对操作者做一些限制,比如只能用户登录且必须是本用户才能进行删除的操作。这就是权限。因此在视图中进行简...