转载

如果你也想写Flask

上次我在编程派发表了 一篇关于如何备份文件至七牛的文章 ,不说好坏,文章发表出去之后我思考了很多,最重要的一点是:如果我是读者,我会愿意阅读完整这篇文章吗?

如果自己是读者该怎样去文章,我是为了什么去读文章,读文章之后我能获得什么?解决现有的问题?还是照搬一些代码到自己的程序上?我们到底为什么要写代码和阅读?我最近一直在思考这些问题。

所以,今天我想以一个全新的视角去写一篇文章,也许会不成功。但我想尝试一下不同的写作视角。

本文的标题是《如果你也在写Flask》。顾名思义,这是一篇关于Flask开发的文章。本文将包含但不限于以下内容:

  • Flask是什么
  • 使用Flask制作自己的博客
  • 扩展性到底在哪里
  • 从0到1该如何学习

Flask是什么

Flask是由Python语言编写开发而成的 轻量级 Web服务框架,Flask是由Armin Ronacher制造的一个愚人节玩笑而发展至今。关于Flask更多权威的介绍请访问Wiki浏览- 点击访问 。

EarlGrey:我在这篇文章 >>> 这可能是开发者社区最成功的愚人节玩笑 中较为详细地介绍了Flask背后的由来。

我心中的Flask是什么

Flask的快捷轻便可扩展性高的优点,可供于我想到什么去开发什么的想法,我不用考虑太多。只用想现在我可能想要做一个什么了,那么Flask就可以做到。Flask拥有太多扩展包,你只需要了解这些扩展包的使用方法就可以做到很多你意想不到的功能。

Flask是基于Python所编写的快捷Web框架,那出现一个疑问了,Flask和Python到底有多大的关联?这是一个很深入的问题,而我的理解是Flask即Python,Python非Flask,而我也不会去解释为什么。因为这是每个人的看法,我不想每位看文章的朋友因为阅读了我的文章就给思维上了个锁,我发现太多文章都时读者在阅读的过程中把思维给锁住了,这非常影响阅读者的思考。

当然,我并不是说Flask不可以构建大型项目,而大型项目的构建准备工作需要的更多,这些并不在本文的讨论范围之内。以后如果有时间可以跟大家再来探讨“该怎样去思考构建大型项目”。

现在,请思考你对于Flask的理解是?

使用Flask开发自己的博客

本章可能会遇到很多困难,在实际操作过程当中如果遇到问题,建议使用Google去搜索问题,搜索时注意关键词的使用可以有助你更好寻找到问题的答案,例如搜索“Flask-Login 文档”,“Flask-SQLalchemy 字段说明”等等……又或者给我发mail或者去sf.gg问一下问题,发问时请不要用一些愚蠢的提问方式例如“请问我这里是哪里错了?”,“这个错误该如何解决?”,我们应在标题写入对问题的精要部分,使得为你解决问题的人们更有兴致的帮助你。

这里我推荐一本书 学会提问 ,如果你有时间可以下载到Kindle或者手机阅读,你当然可以作为厕所读物。慢慢看,细细品。

好了,进入正题,构建Flask-Blog的流程分为以下步骤:

  1. 搭建Flask开发环境 / 完全说明
  2. 思考数据库模型 / 完全说明
  3. 编写逻辑代码 / 完全说明
  4. 测试、完善 / 简单描述
  5. 部署到公网服务器 / 简单描述
  6. 开始扩展你的Blog程序/ 自己完成

所有步骤的注明,会和本文密切相关。我希望读者们可以边看边互动,这样才会有学习的意义。

搭建Flask开发环境

无论你是使用Linux,Mac,Windows搭建环境都是很轻松的,阅读 官方文档 就能做到环境的部署这里我在说明一遍。

安装virtualenv

Linux and Mac:

sudo pip install virtualenv

Windows:

首先需要把Python根目录下的Script目录指定到系统PATH内,然后执行。最重要的是,你需要在windows下安装GIT,利用Git bash来代替原始CMD。

easy_install pip  #安装pip pip install virtualenv

创建项目文件夹

Mac、Linux

mkdir -p ~/Document/flask-bb & cd ~/Document/flask-bb #创建文件夹并移动到文件夹 virtualenv venv #创建virtualenv独立环境

Windows

在你想要的盘符创建一个文件夹名为flask-bb,路径中不要带中文。

在项目文件夹内右击选择Git Bash Here virtualenv venv #创建virtualenv独立环境

使用virtualenv

Mac、Linux在项目目录下输入

. venv/bin/activate #注意前面有个 . 并空格 终端会进入virtualenv环境,并在提示符最前面加入(venv)

Windows系统下,同样在项目文件夹内打开Git Bash

. venv/Script/activate <span>#注意 . 和空格</span>

现在我们已经成功的部署并使用到了virtualenv环境,至于virtualenv到底有什么用呢?它其实就是一个便捷的Python虚拟环境,因为Flask的特性,每个项目里都会有不同扩展包来扩展项目本身。为了洁癖精神,不把每一个使用的扩展包都安装到 根Python环境 里。所以我们进行了一个小型的虚拟Python环境,让这些 针对于 当前项目的Flask扩展包得以应用安装。

提示: virtualenv 不仅仅不适用于Flask,还可以是任何Python的开发环境,只要你有需求。你可以针对你不同的项目设定不同的virtualenv环境。

你需要一个数据库

在本文中,我选择使用Mysql作为数据库,虽然sqlite更简单,可随意创建文件,有了问题直接删除。但为了我们在本地开发和实际部署在生产保持数据库的一致,所以在本地和服务器上都使用Mysql,而基于Mysql的GUI管理软件也有很多,大家自行搜索一下就可以在自己的系统环境中安装好Mysql。

本次我使用的是Mysql 5.6版本,并创建一个数据库编码格式为 utf8mb4 默认排序规则为 utf8mb4_bin 的数据库表。建议创建一个新的账户来管理此数据库表。

运行Flask

部署好Flask环境后,我们需要怎样去运用它呢?

首先我们需要安装Flask。进入virtualenv环境后,运行如下命令:

pip install flask   <span>#安装Flask</span> pip install flask-script    <span>#安装Flask-Script 来代替原生启动管理</span> pip install flask-SQLAlchemy    <span>#安装Flask-SQLAlchemy来管理数据库</span> pip install mysql-python    <span>#安装mysql-python驱动数据库</span>

接下来要特别注意,对于我们现在所需求的FlaskBlog的全部功能的代码都可以写到一个.py文件里,但我非常不推荐这样(我相信也没人会推荐这么做)。如果这样写非常不便于扩展功能面,本文全部描述的功能虽然仅限于非常基础的内容,但想要扩展是非常容易的,而把整个项目的文件及文件夹规划好了,更便于我们后期再次扩展开发时的效率!

以下文章所有文件,我会基于根目录来标注文件路径。

例如:

/config.py 则在项目目录根上。

/app/main.py 则在项目目录创建一个app的文件夹下创建main.py

开始编写吧!

/config.py

<span># -*- coding=utf-8 -*-</span>  <span>'''</span> <span>要注意的是,这里可以写入多个配置,就仿照DevelopmentConfig这个类一样,继承Config类即可。</span> <span>并在最下方的Config字典里添加对应的key:value。</span> <span>'''</span>  <span>class</span> <span>Config</span><span>:</span>     <span>SECRET_KEY</span> <span>=</span> <span>''</span> <span>#填入密钥</span>     <span>SQLALCHEMY_COMMIT_ON_TEARDOWN</span> <span>=</span> <span>True</span>     <span>SQLALCHEMY_TRACK_MODIFICATIONS</span> <span>=</span> <span>True</span>      <span>@staticmethod</span>     <span>def</span> <span>init_app</span><span>(</span><span>app</span><span>):</span>         <span>_handler</span> <span>=</span> <span>RotatingFileHandler</span><span>(</span>             <span>'app.log'</span><span>,</span> <span>maxBytes</span><span>=</span><span>10000</span><span>,</span> <span>backupCount</span><span>=</span><span>1</span><span>)</span>         <span>_handler</span><span>.</span><span>setLevel</span><span>(</span><span>logging</span><span>.</span><span>WARNING</span><span>)</span>         <span>app</span><span>.</span><span>logger</span><span>.</span><span>addHandler</span><span>(</span><span>_handler</span><span>)</span>  <span>class</span> <span>DevelopmentConfig</span><span>(</span><span>Config</span><span>):</span>     <span>SQLALCHEMY_DATABASE_URI</span> <span>=</span> <span>'mysql://flask:flask@127.0.0.1/flask_dev'</span>     <span>#SQLALCHEMY链接数据库都是以URI方式格式为'mysql://账户名:密码@地址/数据库表名'</span>   <span>config</span> <span>=</span> <span>{</span>     <span>'default'</span><span>:</span> <span>DevelopmentConfig</span> <span>}</span>

/manage.py

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>app</span> <span>import</span> <span>create_app</span><span>,</span> <span>db</span> <span>from</span> <span>flask.ext.script</span> <span>import</span> <span>Manager</span><span>,</span> <span>Shell</span>  <span>app</span> <span>=</span> <span>create_app</span><span>(</span><span>'default'</span><span>)</span> <span>manager</span> <span>=</span> <span>Manager</span><span>(</span><span>app</span><span>)</span>  <span>if</span> <span>__name__</span> <span>==</span> <span>'__main__'</span><span>:</span>     <span>manager</span><span>.</span><span>run</span><span>()</span>

/app/__init__.py

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>flask</span> <span>import</span> <span>Flask</span> <span>from</span> <span>flask.ext.sqlalchemy</span> <span>import</span> <span>SQLAlchemy</span> <span>from</span> <span>config</span> <span>import</span> <span>config</span>  <span>db</span> <span>=</span> <span>SQLAlchemy</span><span>()</span>   <span>def</span> <span>create_app</span><span>(</span><span>config_name</span><span>):</span>     <span>app</span> <span>=</span> <span>Flask</span><span>(</span><span>__name__</span><span>)</span>     <span>app</span><span>.</span><span>config</span><span>.</span><span>from_object</span><span>(</span><span>config</span><span>[</span><span>config_name</span><span>])</span>     <span>config</span><span>[</span><span>config_name</span><span>]</span><span>.</span><span>init_app</span><span>(</span><span>app</span><span>)</span>      <span>db</span><span>.</span><span>init_app</span><span>(</span><span>app</span><span>)</span>      <span>from</span> <span>.main</span> <span>import</span> <span>main</span> <span>as</span> <span>main_blueprint</span>     <span>app</span><span>.</span><span>register_blueprint</span><span>(</span><span>main_blueprint</span><span>)</span>      <span>return</span> <span>app</span>

/app/main/__init__.py

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>flask</span> <span>import</span> <span>Blueprint</span>  <span>main</span> <span>=</span> <span>Blueprint</span><span>(</span><span>'main'</span><span>,</span> <span>__name__</span><span>)</span>  <span>from</span> <span>.</span> <span>import</span> <span>views</span><span>,</span> <span>errors</span>

/app/errors.py #创建该文件

/app/views.py

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>.</span> <span>import</span> <span>main</span>   <span>@main.route</span><span>(</span><span>'/'</span><span>)</span> <span>def</span> <span>index</span><span>():</span>     <span>return</span> <span>'Hello World'</span>

现在,在项目根目录下执行 python manage.py runserver 即可成功运行Flask,并访问127.0.0.1:5000。

好了,我们完成了第一步,在第一步中我们使用Flask的特性 蓝图 实现模块化应用,例如我们刚创建的蓝图 main 就作为我们展示模块,当有访客浏览我们Blog的时候都是 main 在工作,而我们在后台编写文章时就不能使用该蓝图了,应该创建一个新的如 admin 来模块化运行。蓝图有很多特性,可以点击上方的链接浏览官方蓝图文档来让自己更了解蓝图的特性。

思考数据库模型

开始想象

为什么这一段我要让大家开始想象数据库模型,无论你是否有过开发经验,对数据库的了解或对任何的了解。你在一开始都要定义数据库模型,现在我们就开始想象一下到底需要哪些数据库模型。

Blog都有哪些内容? 站点名字、介绍、文章标题、内容、分类、评论、标签。 嗯,还得带个用户。~~要不然怎么识别自己是管理员呢?总不能输入一个通用密码登入吧?对这里提醒了我们,一个人的Blog到底需不需要所谓的“管理员账户”既然只有我们知道管理员地址,也只有我一个人发表文章,为什么我们还需要用户呢?我们形成一个专有密码不就可以作为后台登入的钥匙了吗?也许这会降低所谓的安全性,但我认为是一个非常好的想法。你既然都是一个人写了,干嘛不要更简单点的登入模式?

如删除线内容一样,我在写文章的时候最初考虑的是用一个简单的一串密钥密码来验证后台管理,但我在写的过程当中考虑到了为了能在本文中对于用户登入注册这一块产生一块小的思考点,会使读者在后期自己开发时有更好的思维方向。

OK,我来一点一点解释需求的字段到底该不该保留。站点名字和介绍我们可以写在Base.html模板里把,文章标题、分类、内容、标签是需要的,但评论是否需要吗?现在有很多评论系统,国内的 多说 ,国外的 Disqus 都可以引用到Blog里作为评论,那我是个懒人不想写标签这一块的字段,就留给你们最后去自己思考怎么写吧。

按我上面所说的,我们定义一个极简Blog大概就需要3张表“文章表”、“分类表”、“用户表”用来储存标题、内容、分类然后进行关联,而登入我们使用flask-login扩展来帮助我们完成自己定义好模型即可,评论用第三方扩展来处理,标签自己想办法。

注:不要被我的思维而定死了,请尽情想象,如果手边有笔纸我建议你写下来。并用关联线去自由的关联你想要产生出关系的数据库字段。

文章表

字段名 属性 说明
id 数字 主键字段,自增
title 文本 标题字段
body 多行文本 内容字段
create_time 时间 创建文章时间
user_id 数字 关联用户表主键ID
category_id 数字 关联分类表主键ID

分类表

字段名 属性 说明
id 数字 主键字段,自增
name 文本 分类名字

用户表

字段名 属性 说明
id 数字 主键字段,自增
username string 用户名字段
password string 密码字段
real_name string 真实姓名

在三个表之间我们有两个一对多的关系,也就是一(分类)对多(文章)。现在再想想有什么更好的点子?或者你需要的字段更多?这些你都可以尽可能的发挥自己的想象及联合你本身实际的需求来增减。

那么我们把字段定出来了,怎么写到Flask生成数据库呢?

/app/models.py

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>.</span> <span>import</span> <span>db</span> <span>from</span> <span>datetime</span> <span>import</span> <span>datetime</span>   <span>class</span> <span>Article</span><span>(</span><span>db</span><span>.</span><span>Model</span><span>):</span>     <span>__tablename__</span> <span>=</span> <span>'articles'</span>     <span>id</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Integer</span><span>,</span> <span>primary_key</span><span>=</span><span>True</span><span>)</span>     <span>titile</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>String</span><span>(</span><span>64</span><span>),</span> <span>unique</span><span>=</span><span>True</span><span>)</span>     <span>body</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Text</span><span>)</span>     <span>create_time</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>DATETIME</span><span>,</span> <span>default</span><span>=</span><span>datetime</span><span>.</span><span>utcnow</span><span>())</span>     <span>category_id</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Integer</span><span>,</span> <span>db</span><span>.</span><span>ForeignKey</span><span>(</span><span>'categorys.id'</span><span>))</span>     <span>user_id</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Integer</span><span>,</span> <span>db</span><span>.</span><span>ForeignKey</span><span>(</span><span>'users.id'</span><span>))</span>   <span>class</span> <span>Category</span><span>(</span><span>db</span><span>.</span><span>Model</span><span>):</span>     <span>__tablename__</span> <span>=</span> <span>'categorys'</span>     <span>id</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Integer</span><span>,</span> <span>primary_key</span><span>=</span><span>True</span><span>)</span>     <span>name</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>String</span><span>(</span><span>64</span><span>),</span> <span>unique</span><span>=</span><span>True</span><span>)</span>     <span>articles</span> <span>=</span> <span>db</span><span>.</span><span>relationship</span><span>(</span><span>'Article'</span><span>,</span> <span>backref</span><span>=</span><span>'category'</span><span>)</span>   <span>class</span> <span>User</span><span>(</span><span>db</span><span>.</span><span>Model</span><span>):</span>     <span>__tablename__</span> <span>=</span> <span>'users'</span>     <span>id</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Integer</span><span>,</span> <span>primary_key</span><span>=</span><span>True</span><span>)</span>     <span>username</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>String</span><span>(</span><span>64</span><span>),</span> <span>unique</span><span>=</span><span>True</span><span>)</span>     <span>password_hash</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>String</span><span>(</span><span>128</span><span>))</span>     <span>real_name</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>String</span><span>(</span><span>64</span><span>),</span> <span>unique</span><span>=</span><span>True</span><span>)</span>     <span>articles</span> <span>=</span> <span>db</span><span>.</span><span>relationship</span><span>(</span><span>'Article'</span><span>,</span> <span>backref</span><span>=</span><span>'user'</span><span>)</span>

我写了三个类,分别为文章、分类、用户,其中有两条一对多的关系。 关于一对多、多对多的关系详细解释可以去 官方文档 进行查阅了解。 接下来我们来把我们定义好的数据库表写入到数据库。

pip安装

pip install flask-migrate #使用migrate来管理升级迁移数据库

编辑 /manage.py

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>app</span> <span>import</span> <span>create_app</span><span>,</span> <span>db</span> <span>from</span> <span>app.models</span> <span>import</span> <span>Article</span> <span>,</span> <span>Category</span> <span>,</span> <span>User</span>  <span>#注册数据库模型</span> <span>from</span> <span>flask.ext.script</span> <span>import</span> <span>Manager</span><span>,</span> <span>Shell</span> <span>from</span> <span>flask.ext.migrate</span> <span>import</span> <span>Migrate</span><span>,</span> <span>MigrateCommand</span>   <span>#载入migrate扩展</span>  <span>app</span> <span>=</span> <span>create_app</span><span>(</span><span>'default'</span><span>)</span> <span>manager</span> <span>=</span> <span>Manager</span><span>(</span><span>app</span><span>)</span> <span>migrate</span> <span>=</span> <span>Migrate</span><span>(</span><span>app</span><span>,</span> <span>db</span><span>)</span>  <span>#注册migrate到flask</span>   <span>manager</span><span>.</span><span>add_command</span><span>(</span><span>'db'</span><span>,</span> <span>MigrateCommand</span><span>)</span>   <span>#在终端环境下添加一个db命令</span>  <span>if</span> <span>__name__</span> <span>==</span> <span>'__main__'</span><span>:</span>     <span>manager</span><span>.</span><span>run</span><span>()</span>

接下来,我们到终端里依次输入

python manage.py db init python manage.py db migrate -m &quot;first init db&quot; python manage.py db upgrade ''' 单项目内只需要第一次执行db init,如果你在未来的日子里需要修改models.py并使其生效,只需要在改过models.py后执行指令的后面2步即可。 '''

现在你用任何一个可视化或终端去检查一下mysql数据库是否成功创建了数据库表。

顺带我解释一下unique、primary_key、default这些关键字:

unique是否允许重复,如果为True则在该表内不允许该字段用重复的数据出现。 primary_key 是否为主键 default 默认值

到这里,我们已经完成了两项任务。从中我们学习到了如何用virtualenv来部署flask环境,学习到了蓝图模块及设计数据库。我希望到这里你的数据库跟我设计的不一样,实例代码只是为了完成演示步骤。而在阅读的你,我希望你真的能够通过自己的想象去扩建一些字段,并自己理解字段中一些关键词的意义。

编写逻辑代码

设想一下,我们大概需要哪些逻辑代码,呐我想想。

  1. 文章展示页
  2. 文章详情页
  3. 后台登入
  4. 增、改、删文章、分类

在这里我只提供增、删的代码块,改这个代码块作为练习题由读者自己完成。

喏,这些应该就差不多了吧。 一步一步来,按道理我们应该先写后台。有后台,就应该有注册和登入,那注册开放了,怎么去解决任何人都可以注册的问题呢?有2种办法。

  1. 拥有注册码的才可以注册(你可以在文件内自定一串随机码作为注册码)

  2. 你可以完成注册后,注释掉注册逻辑代码。

当然这里我推荐第一种啦,因为比较简单,而且如果你想和你的朋友一起写这个博客,就不需要那么麻烦。只需要在告诉他你的注册码是多少就行了。

写好了后台之后,我们就应该要能增、改、删文章或者分类,我们来一步一步设计我们所需求的功能。

编写后台模块

pip安装

pip install flask-wtf   #安装flask-wtf表单快速渲染生成 pip install WTForms-SQLAlchemy #安装flask-wtf-sqlalchemy用于通过数据库数据返回生成表单内容 pip install flask-bootstrap #安装flask-bootstrap,快速渲染bootstrap样式页面。 pip install flask-login #安装flask-login 用于登入及权限管理的扩展

修改-&gt; /app/__init__.py

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>flask</span> <span>import</span> <span>Flask</span> <span>from</span> <span>flask.ext.sqlalchemy</span> <span>import</span> <span>SQLAlchemy</span> <span>from</span> <span>flask.ext.bootstrap</span> <span>import</span> <span>Bootstrap</span>   <span>#引入Flask-Bootstrap</span> <span>from</span> <span>flask.ext.login</span> <span>import</span> <span>LoginManager</span>    <span>#引入Flask-Login</span> <span>from</span> <span>config</span> <span>import</span> <span>config</span>  <span>db</span> <span>=</span> <span>SQLAlchemy</span><span>()</span>   <span>#实例化对象</span> <span>bootstrap</span> <span>=</span> <span>Bootstrap</span><span>()</span> <span>#实例化对象</span> <span>login_manager</span> <span>=</span> <span>LoginManager</span><span>()</span>  <span>#实例化对象</span> <span>login_manager</span><span>.</span><span>session_protection</span> <span>=</span> <span>'strong'</span> <span>#设置flask-login session等级</span> <span>login_manager</span><span>.</span><span>login_view</span> <span>=</span> <span>'admin.login'</span>    <span>#如果未登入转跳到指定方法</span> <span>login_manager</span><span>.</span><span>login_message</span> <span>=</span> <span>u'请登入账号再进行下一步操作!'</span> <span>#未登入提示语</span>  <span>def</span> <span>create_app</span><span>(</span><span>config_name</span><span>):</span>     <span>app</span> <span>=</span> <span>Flask</span><span>(</span><span>__name__</span><span>)</span>     <span>app</span><span>.</span><span>config</span><span>.</span><span>from_object</span><span>(</span><span>config</span><span>[</span><span>config_name</span><span>])</span>     <span>config</span><span>[</span><span>config_name</span><span>]</span><span>.</span><span>init_app</span><span>(</span><span>app</span><span>)</span>      <span>db</span><span>.</span><span>init_app</span><span>(</span><span>app</span><span>)</span>     <span>bootstrap</span><span>.</span><span>init_app</span><span>(</span><span>app</span><span>)</span>     <span>login_manager</span><span>.</span><span>init_app</span><span>(</span><span>app</span><span>)</span>      <span>from</span> <span>.main</span> <span>import</span> <span>main</span> <span>as</span> <span>main_blueprint</span>     <span>app</span><span>.</span><span>register_blueprint</span><span>(</span><span>main_blueprint</span><span>)</span>      <span>from</span> <span>.admin</span> <span>import</span> <span>admin</span> <span>as</span> <span>admin_blueprint</span>     <span>app</span><span>.</span><span>register_blueprint</span><span>(</span><span>admin_blueprint</span><span>,</span> <span>url_prefix</span><span>=</span><span>'/admin'</span><span>)</span>     <span>'''</span> <span>    载入一个名为'admin'的蓝图作为后台管理模块</span> <span>    '''</span>      <span>return</span> <span>app</span>

新建-&gt; /app/admin/__init__.py

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>flask</span> <span>import</span> <span>Blueprint</span>  <span>admin</span> <span>=</span> <span>Blueprint</span><span>(</span><span>'admin'</span><span>,</span> <span>__name__</span><span>)</span>  <span>from</span> <span>.</span> <span>import</span> <span>views</span><span>,</span> <span>errors</span> <span>'''</span> <span>前面我们载入了一个名为admin的蓝图模块</span> <span>在这里我们需要构建这个模块</span> <span>'''</span>

新建-&gt; /app/admin/forms.py

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>flask.ext.wtf</span> <span>import</span> <span>Form</span> <span>from</span> <span>..models</span> <span>import</span> <span>Category</span> <span>from</span> <span>wtforms</span> <span>import</span> <span>StringField</span><span>,</span> <span>SubmitField</span><span>,</span> <span>PasswordField</span><span>,</span> <span>TextAreaField</span> <span>from</span> <span>wtforms.validators</span> <span>import</span> <span>Required</span><span>,</span> <span>length</span><span>,</span> <span>Regexp</span><span>,</span> <span>EqualTo</span> <span>from</span> <span>wtforms.ext.sqlalchemy.fields</span> <span>import</span> <span>QuerySelectField</span>   <span>class</span> <span>LoginForm</span><span>(</span><span>Form</span><span>):</span>     <span>username</span> <span>=</span> <span>StringField</span><span>(</span><span>u'帐号'</span><span>,</span> <span>validators</span><span>=</span><span>[</span><span>Required</span><span>(),</span> <span>length</span><span>(</span><span>6</span><span>,</span> <span>64</span><span>)])</span>     <span>password</span> <span>=</span> <span>PasswordField</span><span>(</span><span>u'密码'</span><span>,</span> <span>validators</span><span>=</span><span>[</span><span>Required</span><span>()])</span>     <span>submit</span> <span>=</span> <span>SubmitField</span><span>(</span><span>u'登入'</span><span>)</span>   <span>class</span> <span>RegistrationForm</span><span>(</span><span>Form</span><span>):</span>     <span>username</span> <span>=</span> <span>StringField</span><span>(</span><span>u'用户名'</span><span>,</span> <span>validators</span><span>=</span><span>[</span><span>Required</span><span>(),</span> <span>length</span><span>(</span><span>6</span><span>,</span> <span>18</span><span>),</span> <span>Regexp</span><span>(</span><span>'^[A-Za-z][A-Za-z0-9_.]*$'</span><span>,</span> <span>0</span><span>,</span> <span>u'用户名只允许字母'</span><span>,</span><span>u'用户名不允许特殊符号'</span><span>)])</span>     <span>password</span> <span>=</span> <span>PasswordField</span><span>(</span>         <span>u'密码'</span><span>,</span> <span>validators</span><span>=</span><span>[</span><span>Required</span><span>(),</span> <span>EqualTo</span><span>(</span><span>'password2'</span><span>,</span> <span>message</span><span>=</span><span>u'密码错误提示1'</span><span>)])</span>     <span>password2</span> <span>=</span> <span>PasswordField</span><span>(</span><span>u'重复密码'</span><span>,</span> <span>validators</span><span>=</span><span>[</span><span>Required</span><span>()])</span>     <span>real_name</span> <span>=</span> <span>StringField</span><span>(</span><span>u'昵称'</span><span>,</span> <span>validators</span><span>=</span><span>[</span><span>Required</span><span>()])</span>     <span>registerkey</span> <span>=</span> <span>StringField</span><span>(</span><span>u'注册码'</span><span>,</span> <span>validators</span><span>=</span><span>[</span><span>Required</span><span>()])</span>     <span>submit</span> <span>=</span> <span>SubmitField</span><span>(</span><span>u'注册'</span><span>)</span>   <span>class</span> <span>PostArticleForm</span><span>(</span><span>Form</span><span>):</span>     <span>title</span> <span>=</span> <span>StringField</span><span>(</span><span>u'标题'</span><span>,</span> <span>validators</span><span>=</span><span>[</span><span>Required</span><span>(),</span> <span>length</span><span>(</span><span>6</span><span>,</span> <span>64</span><span>)])</span>     <span>body</span> <span>=</span> <span>TextAreaField</span><span>(</span><span>u'内容'</span><span>)</span>     <span>category_id</span> <span>=</span> <span>QuerySelectField</span><span>(</span><span>u'分类'</span><span>,</span> <span>query_factory</span><span>=</span><span>lambda</span><span>:</span> <span>Category</span><span>.</span><span>query</span><span>.</span><span>all</span><span>(</span>     <span>),</span> <span>get_pk</span><span>=</span><span>lambda</span> <span>a</span><span>:</span> <span>str</span><span>(</span><span>a</span><span>.</span><span>id</span><span>),</span> <span>get_label</span><span>=</span><span>lambda</span> <span>a</span><span>:</span> <span>a</span><span>.</span><span>name</span><span>)</span>     <span>submit</span> <span>=</span> <span>SubmitField</span><span>(</span><span>u'发布'</span><span>)</span>   <span>class</span> <span>PostCategoryForm</span><span>(</span><span>Form</span><span>):</span>     <span>name</span> <span>=</span> <span>StringField</span><span>(</span><span>u'分类名'</span><span>,</span> <span>validators</span><span>=</span><span>[</span><span>Required</span><span>(),</span> <span>length</span><span>(</span><span>6</span><span>,</span> <span>64</span><span>)])</span>     <span>submit</span> <span>=</span> <span>SubmitField</span><span>(</span><span>u'发布'</span><span>)</span>

forms.py里构建表单内容,然后在前端渲染时直接调用对应form即可快速生成表单内容。

PostArticleForm 表单里我们更引入了wtf-sqlalchemy的特性,通过查找数据库内容来生成对应的select表单。

  • 修改-&gt; /app/models.py *
<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>.</span> <span>import</span> <span>db</span><span>,</span> <span>login_manager</span> <span>from</span> <span>datetime</span> <span>import</span> <span>datetime</span> <span>from</span> <span>werkzeug.security</span> <span>import</span> <span>generate_password_hash</span><span>,</span> <span>check_password_hash</span>  <span># 引入密码加密 验证方法</span> <span>from</span> <span>flask.ext.login</span> <span>import</span> <span>UserMixin</span>  <span># 引入flask-login用户模型继承类方法</span>   <span>class</span> <span>Article</span><span>(</span><span>db</span><span>.</span><span>Model</span><span>):</span>     <span>__tablename__</span> <span>=</span> <span>'articles'</span>     <span>id</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Integer</span><span>,</span> <span>primary_key</span><span>=</span><span>True</span><span>)</span>     <span>title</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>String</span><span>(</span><span>64</span><span>),</span> <span>unique</span><span>=</span><span>True</span><span>)</span>     <span>body</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Text</span><span>)</span>     <span>create_time</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>DATETIME</span><span>,</span> <span>default</span><span>=</span><span>datetime</span><span>.</span><span>utcnow</span><span>())</span>     <span>category_id</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Integer</span><span>,</span> <span>db</span><span>.</span><span>ForeignKey</span><span>(</span><span>'categorys.id'</span><span>))</span>     <span>user_id</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Integer</span><span>,</span> <span>db</span><span>.</span><span>ForeignKey</span><span>(</span><span>'users.id'</span><span>))</span>   <span>class</span> <span>Category</span><span>(</span><span>db</span><span>.</span><span>Model</span><span>):</span>     <span>__tablename__</span> <span>=</span> <span>'categorys'</span>     <span>id</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Integer</span><span>,</span> <span>primary_key</span><span>=</span><span>True</span><span>)</span>     <span>name</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>String</span><span>(</span><span>64</span><span>),</span> <span>unique</span><span>=</span><span>True</span><span>)</span>     <span>articles</span> <span>=</span> <span>db</span><span>.</span><span>relationship</span><span>(</span><span>'Article'</span><span>,</span> <span>backref</span><span>=</span><span>'category'</span><span>)</span>   <span>class</span> <span>User</span><span>(</span><span>UserMixin</span><span>,</span> <span>db</span><span>.</span><span>Model</span><span>):</span>     <span># 在使用Flask-Login作为登入功能时,在user模型要继承UserMimix类.</span>     <span>__tablename__</span> <span>=</span> <span>'users'</span>     <span>id</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>Integer</span><span>,</span> <span>primary_key</span><span>=</span><span>True</span><span>)</span>     <span>username</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>String</span><span>(</span><span>64</span><span>),</span> <span>unique</span><span>=</span><span>True</span><span>)</span>     <span>password_hash</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>String</span><span>(</span><span>128</span><span>))</span>     <span>real_name</span> <span>=</span> <span>db</span><span>.</span><span>Column</span><span>(</span><span>db</span><span>.</span><span>String</span><span>(</span><span>64</span><span>),</span> <span>unique</span><span>=</span><span>True</span><span>)</span>     <span>articles</span> <span>=</span> <span>db</span><span>.</span><span>relationship</span><span>(</span><span>'Article'</span><span>,</span> <span>backref</span><span>=</span><span>'user'</span><span>)</span>      <span>@property</span>     <span>def</span> <span>password</span><span>(</span><span>self</span><span>):</span>         <span>raise</span> <span>AttributeError</span><span>(</span><span>u'密码属性不正确'</span><span>)</span>      <span>@password.setter</span>     <span>def</span> <span>password</span><span>(</span><span>self</span><span>,</span> <span>password</span><span>):</span>         <span>self</span><span>.</span><span>password_hash</span> <span>=</span> <span>generate_password_hash</span><span>(</span><span>password</span><span>)</span>         <span># 增加password会通过generate_password_hash方法来加密储存</span>      <span>def</span> <span>verify_password</span><span>(</span><span>self</span><span>,</span> <span>password</span><span>):</span>         <span>return</span> <span>check_password_hash</span><span>(</span><span>self</span><span>.</span><span>password_hash</span><span>,</span> <span>password</span><span>)</span>         <span># 在登入时,我们需要验证明文密码是否和加密密码所吻合</span>   <span>@login_manager.user_loader</span> <span>def</span> <span>load_user</span><span>(</span><span>user_id</span><span>):</span>     <span>return</span> <span>User</span><span>.</span><span>query</span><span>.</span><span>get</span><span>(</span><span>int</span><span>(</span><span>user_id</span><span>))</span>

新建-&gt; /app/admin/views.py

编写路由视图代码块,包括登入、注册、增删文章、分类业务。

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>.</span> <span>import</span> <span>admin</span> <span>from</span> <span>flask</span> <span>import</span> <span>render_template</span><span>,</span> <span>flash</span><span>,</span> <span>redirect</span><span>,</span> <span>url_for</span><span>,</span> <span>request</span> <span>from</span> <span>flask.ext.login</span> <span>import</span> <span>login_required</span><span>,</span> <span>current_user</span><span>,</span> <span>login_user</span><span>,</span> <span>logout_user</span> <span>from</span> <span>forms</span> <span>import</span> <span>LoginForm</span><span>,</span> <span>RegistrationForm</span><span>,</span> <span>PostArticleForm</span><span>,</span> <span>PostCategoryForm</span> <span>from</span> <span>..models</span> <span>import</span> <span>User</span><span>,</span> <span>Article</span><span>,</span> <span>Category</span> <span>from</span> <span>..</span> <span>import</span> <span>db</span>   <span>@admin.route</span><span>(</span><span>'/'</span><span>)</span> <span>def</span> <span>index</span><span>():</span>     <span>return</span> <span>render_template</span><span>(</span><span>'admin/index.html'</span><span>)</span>   <span>@admin.route</span><span>(</span><span>'/login'</span><span>,</span> <span>methods</span><span>=</span><span>[</span><span>'GET'</span><span>,</span> <span>'POST'</span><span>])</span> <span>def</span> <span>login</span><span>():</span>     <span>form</span> <span>=</span> <span>LoginForm</span><span>()</span>     <span>if</span> <span>form</span><span>.</span><span>validate_on_submit</span><span>():</span>         <span>user</span> <span>=</span> <span>User</span><span>.</span><span>query</span><span>.</span><span>filter_by</span><span>(</span><span>username</span><span>=</span><span>form</span><span>.</span><span>username</span><span>.</span><span>data</span><span>)</span><span>.</span><span>first</span><span>()</span>         <span>if</span> <span>user</span> <span>is</span> <span>not</span> <span>None</span> <span>and</span> <span>user</span><span>.</span><span>verify_password</span><span>(</span><span>form</span><span>.</span><span>password</span><span>.</span><span>data</span><span>):</span>             <span>login_user</span><span>(</span><span>user</span><span>)</span>             <span>return</span> <span>redirect</span><span>(</span><span>request</span><span>.</span><span>args</span><span>.</span><span>get</span><span>(</span><span>'next'</span><span>)</span> <span>or</span> <span>url_for</span><span>(</span><span>'admin.index'</span><span>))</span>         <span>flash</span><span>(</span><span>u'用户密码不正确'</span><span>)</span>     <span>return</span> <span>render_template</span><span>(</span><span>'admin/login.html'</span><span>,</span> <span>form</span><span>=</span><span>form</span><span>)</span>   <span>@admin.route</span><span>(</span><span>'/register'</span><span>,</span> <span>methods</span><span>=</span><span>[</span><span>'GET'</span><span>,</span> <span>'POST'</span><span>])</span> <span>def</span> <span>register</span><span>():</span>     <span>register_key</span> <span>=</span> <span>'zhucema'</span>     <span>form</span> <span>=</span> <span>RegistrationForm</span><span>()</span>     <span>if</span> <span>form</span><span>.</span><span>validate_on_submit</span><span>():</span>         <span>if</span> <span>form</span><span>.</span><span>registerkey</span><span>.</span><span>data</span> <span>!=</span> <span>register_key</span><span>:</span>             <span>flash</span><span>(</span><span>u'注册码不符,请返回重试.'</span><span>)</span>             <span>return</span> <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'admin.register'</span><span>))</span>         <span>else</span><span>:</span>             <span>if</span> <span>form</span><span>.</span><span>password</span><span>.</span><span>data</span> <span>!=</span> <span>form</span><span>.</span><span>password2</span><span>.</span><span>data</span><span>:</span>                 <span>flash</span><span>(</span><span>u'两次输入密码不一'</span><span>)</span>                 <span>return</span> <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'admin.register'</span><span>))</span>             <span>else</span><span>:</span>                 <span>user</span> <span>=</span> <span>User</span><span>(</span><span>username</span><span>=</span><span>form</span><span>.</span><span>username</span><span>.</span><span>data</span><span>,</span> <span>password</span><span>=</span><span>form</span><span>.</span><span>password</span><span>.</span><span>data</span><span>,</span> <span>real_name</span><span>=</span><span>form</span><span>.</span><span>real_name</span><span>.</span><span>data</span><span>)</span>                 <span>db</span><span>.</span><span>session</span><span>.</span><span>add</span><span>(</span><span>user</span><span>)</span>                 <span>flash</span><span>(</span><span>u'您已经注册成功'</span><span>)</span>                 <span>return</span> <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'admin.login'</span><span>))</span>     <span>return</span> <span>render_template</span><span>(</span><span>'admin/register.html'</span><span>,</span> <span>form</span><span>=</span><span>form</span><span>)</span>   <span>@admin.route</span><span>(</span><span>'/logout'</span><span>)</span> <span>@login_required</span> <span>def</span> <span>logout</span><span>():</span>     <span>logout_user</span><span>()</span>     <span>flash</span><span>(</span><span>u'您已经登出了系统'</span><span>)</span>     <span>return</span> <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'main.index'</span><span>))</span>   <span>@admin.route</span><span>(</span><span>'/article'</span><span>,</span> <span>methods</span><span>=</span><span>[</span><span>'GET'</span><span>,</span> <span>'POST'</span><span>])</span> <span>@login_required</span> <span>def</span> <span>article</span><span>():</span>     <span>form</span> <span>=</span> <span>PostArticleForm</span><span>()</span>     <span>alist</span> <span>=</span> <span>Article</span><span>.</span><span>query</span><span>.</span><span>all</span><span>()</span>     <span>if</span> <span>form</span><span>.</span><span>validate_on_submit</span><span>():</span>         <span>acticle</span> <span>=</span> <span>Article</span><span>(</span><span>title</span><span>=</span><span>form</span><span>.</span><span>title</span><span>.</span><span>data</span><span>,</span> <span>body</span><span>=</span><span>form</span><span>.</span><span>body</span><span>.</span><span>data</span><span>,</span> <span>category_id</span><span>=</span><span>str</span><span>(</span><span>form</span><span>.</span><span>category_id</span><span>.</span><span>data</span><span>.</span><span>id</span><span>),</span>                           <span>user_id</span><span>=</span><span>current_user</span><span>.</span><span>id</span><span>)</span>         <span>db</span><span>.</span><span>session</span><span>.</span><span>add</span><span>(</span><span>acticle</span><span>)</span>         <span>flash</span><span>(</span><span>u'文章添加成功'</span><span>)</span>         <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'admin.index'</span><span>))</span>     <span>return</span> <span>render_template</span><span>(</span><span>'admin/article.html'</span><span>,</span> <span>form</span><span>=</span><span>form</span><span>,</span> <span>list</span><span>=</span><span>alist</span><span>)</span>   <span>@admin.route</span><span>(</span><span>'/article/del'</span><span>,</span> <span>methods</span><span>=</span><span>[</span><span>'GET'</span><span>])</span> <span>@login_required</span> <span>def</span> <span>article_del</span><span>():</span>     <span>if</span> <span>request</span><span>.</span><span>args</span><span>.</span><span>get</span><span>(</span><span>'id'</span><span>)</span> <span>is</span> <span>not</span> <span>None</span> <span>and</span> <span>request</span><span>.</span><span>args</span><span>.</span><span>get</span><span>(</span><span>'a'</span><span>)</span> <span>==</span> <span>'del'</span><span>:</span>         <span>x</span> <span>=</span> <span>Article</span><span>.</span><span>query</span><span>.</span><span>filter_by</span><span>(</span><span>id</span><span>=</span><span>request</span><span>.</span><span>args</span><span>.</span><span>get</span><span>(</span><span>'id'</span><span>))</span><span>.</span><span>first</span><span>()</span>         <span>if</span> <span>x</span> <span>is</span> <span>not</span> <span>None</span><span>:</span>             <span>db</span><span>.</span><span>session</span><span>.</span><span>delete</span><span>(</span><span>x</span><span>)</span>             <span>db</span><span>.</span><span>session</span><span>.</span><span>commit</span><span>()</span>             <span>flash</span><span>(</span><span>u'已经删除'</span> <span>+</span> <span>x</span><span>.</span><span>title</span><span>)</span>             <span>return</span> <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'admin.article'</span><span>))</span>         <span>flash</span><span>(</span><span>u'请检查输入'</span><span>)</span>         <span>return</span> <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'admin.article'</span><span>))</span>   <span>@admin.route</span><span>(</span><span>'/category'</span><span>,</span> <span>methods</span><span>=</span><span>[</span><span>'GET'</span><span>,</span> <span>'POST'</span><span>])</span> <span>def</span> <span>category</span><span>():</span>     <span>clist</span> <span>=</span> <span>Category</span><span>.</span><span>query</span><span>.</span><span>all</span><span>()</span>     <span>form</span> <span>=</span> <span>PostCategoryForm</span><span>()</span>     <span>if</span> <span>form</span><span>.</span><span>validate_on_submit</span><span>():</span>         <span>category</span> <span>=</span> <span>Category</span><span>(</span><span>name</span><span>=</span><span>form</span><span>.</span><span>name</span><span>.</span><span>data</span><span>)</span>         <span>db</span><span>.</span><span>session</span><span>.</span><span>add</span><span>(</span><span>category</span><span>)</span>         <span>flash</span><span>(</span><span>u'分类添加成功'</span><span>)</span>         <span>return</span> <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'admin.index'</span><span>))</span>     <span>return</span> <span>render_template</span><span>(</span><span>'admin/category.html'</span><span>,</span> <span>form</span><span>=</span><span>form</span><span>,</span> <span>list</span><span>=</span><span>clist</span><span>)</span>   <span>@admin.route</span><span>(</span><span>'/category/del'</span><span>,</span> <span>methods</span><span>=</span><span>[</span><span>'GET'</span><span>])</span> <span>@login_required</span> <span>def</span> <span>category_del</span><span>():</span>     <span>if</span> <span>request</span><span>.</span><span>args</span><span>.</span><span>get</span><span>(</span><span>'id'</span><span>)</span> <span>is</span> <span>not</span> <span>None</span> <span>and</span> <span>request</span><span>.</span><span>args</span><span>.</span><span>get</span><span>(</span><span>'a'</span><span>)</span> <span>==</span> <span>'del'</span><span>:</span>         <span>x</span> <span>=</span> <span>Category</span><span>.</span><span>query</span><span>.</span><span>filter_by</span><span>(</span><span>id</span><span>=</span><span>request</span><span>.</span><span>args</span><span>.</span><span>get</span><span>(</span><span>'id'</span><span>))</span><span>.</span><span>first</span><span>()</span>         <span>if</span> <span>x</span> <span>is</span> <span>not</span> <span>None</span><span>:</span>             <span>db</span><span>.</span><span>session</span><span>.</span><span>delete</span><span>(</span><span>x</span><span>)</span>             <span>db</span><span>.</span><span>session</span><span>.</span><span>commit</span><span>()</span>             <span>flash</span><span>(</span><span>u'已经删除'</span> <span>+</span> <span>x</span><span>.</span><span>name</span><span>)</span>             <span>return</span> <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'admin.category'</span><span>))</span>         <span>flash</span><span>(</span><span>u'请检查输入'</span><span>)</span>         <span>return</span> <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'admin.category'</span><span>))</span>

新建-&gt; /app/templates/base.html

定义基础模板,从后我们所有的渲染模板都会基于基础模板来继续渲染出内容页面。

{% extends &quot;bootstrap/base.html&quot; %}  {% block title %} Flask-Blog{% endblock %} {% block navbar %}     <span>&lt;div</span> <span>class=</span><span>&quot;navbar navbar-inverse&quot;</span> <span>role=</span><span>&quot;navigation&quot;</span><span>&gt;</span>         <span>&lt;div</span> <span>class=</span><span>&quot;container&quot;</span><span>&gt;</span>             <span>&lt;div</span> <span>class=</span><span>&quot;navbar-header&quot;</span><span>&gt;</span>                 <span>&lt;button</span> <span>type=</span><span>&quot;button&quot;</span> <span>class=</span><span>&quot;navbar-toggle&quot;</span>                         <span>data-toggle=</span><span>&quot;collapse&quot;</span> <span>data-target=</span><span>&quot;.navbar-collapse&quot;</span><span>&gt;</span>                     <span>&lt;span</span> <span>class=</span><span>&quot;sr-only&quot;</span><span>&gt;</span>Toggle navigation<span>&lt;/span&gt;</span>                     <span>&lt;span</span> <span>class=</span><span>&quot;icon-bar&quot;</span><span>&gt;&lt;/span&gt;</span>                     <span>&lt;span</span> <span>class=</span><span>&quot;icon-bar&quot;</span><span>&gt;&lt;/span&gt;</span>                     <span>&lt;span</span> <span>class=</span><span>&quot;icon-bar&quot;</span><span>&gt;&lt;/span&gt;</span>                 <span>&lt;/button&gt;</span>                 <span>&lt;a</span> <span>class=</span><span>&quot;navbar-brand&quot;</span> <span>href=</span><span>&quot;/&quot;</span><span>&gt;</span>Blog<span>&lt;/a&gt;</span>             <span>&lt;/div&gt;</span>             <span>&lt;div</span> <span>class=</span><span>&quot;navbar-collapse collapse&quot;</span><span>&gt;</span>                 <span>&lt;ul</span> <span>class=</span><span>&quot;nav navbar-nav&quot;</span><span>&gt;</span>                     <span>&lt;li&gt;&lt;a</span> <span>href=</span><span>&quot;{{ url_for('main.index') }}&quot;</span><span>&gt;</span>首页<span>&lt;/a&gt;&lt;/li&gt;</span>                     {% if current_user.is_authenticated %}                         <span>&lt;li&gt;&lt;a</span> <span>href=</span><span>&quot;{{ url_for('admin.index') }}&quot;</span><span>&gt;</span>后台首页<span>&lt;/a&gt;&lt;/li&gt;</span>                         <span>&lt;li&gt;&lt;a</span> <span>href=</span><span>&quot;{{ url_for('admin.article') }}&quot;</span><span>&gt;</span>文章<span>&lt;/a&gt;&lt;/li&gt;</span>                         <span>&lt;li&gt;&lt;a</span> <span>href=</span><span>&quot;{{ url_for('admin.category') }}&quot;</span><span>&gt;</span>分类<span>&lt;/a&gt;&lt;/li&gt;</span>                     {% endif %}                 <span>&lt;/ul&gt;</span>                 <span>&lt;ul</span> <span>class=</span><span>&quot;nav navbar-nav navbar-right&quot;</span><span>&gt;</span>                     {% if current_user.is_authenticated %}                         <span>&lt;li&gt;&lt;a</span> <span>href=</span><span>&quot;{{ url_for('admin.logout') }}&quot;</span><span>&gt;</span>登出<span>&lt;/a&gt;&lt;/li&gt;</span>                     {% else %}                         <span>&lt;li&gt;&lt;a</span> <span>href=</span><span>&quot;{{ url_for('admin.register') }}&quot;</span><span>&gt;</span>注册<span>&lt;/a&gt;&lt;/li&gt;</span>                         <span>&lt;li&gt;&lt;a</span> <span>href=</span><span>&quot;{{ url_for('admin.login') }}&quot;</span><span>&gt;</span>登入<span>&lt;/a&gt;&lt;/li&gt;</span>                     {% endif %}                 <span>&lt;/ul&gt;</span>             <span>&lt;/div&gt;</span>         <span>&lt;/div&gt;</span>     <span>&lt;/div&gt;</span> {% endblock %}  {% block content %}     <span>&lt;div</span> <span>class=</span><span>&quot;container&quot;</span><span>&gt;</span>         {% for message in get_flashed_messages() %}             <span>&lt;div</span> <span>class=</span><span>&quot;alert alert-warning&quot;</span><span>&gt;</span>                 <span>&lt;button</span> <span>type=</span><span>&quot;button&quot;</span> <span>class=</span><span>&quot;close&quot;</span> <span>data-dismiss=</span><span>&quot;alert&quot;</span><span>&gt;</span><span>&times;</span><span>&lt;/button&gt;</span>                 {{ message }}             <span>&lt;/div&gt;</span>         {% endfor %}         {% block page_content %}         {% endblock %}     <span>&lt;/div&gt;</span> {% endblock %}

在base.html里,运用到了jinja模板引擎的if语法,并检测用户是否登入。如果登入则显示后台管理链接。并判断状态显示注册、登入还是登出。

新建-&gt; /app/templates/admin/register.html

前端注册页面直接继承base.html,并在base.html的原有基础上增加渲染表单,我们在register视图里返回结果时,增加了注册表单的返回,所以在前端页面直接使用flask-wtf特性渲染出来即可使用。

{% extends &quot;base.html&quot; %} {% import &quot;bootstrap/wtf.html&quot; as wtf %}   {% block page_content %}     <span>&lt;div</span> <span>class=</span><span>&quot;col-md-6&quot;</span><span>&gt;</span>         <span>&lt;h1&gt;</span>注册账户<span>&lt;/h1&gt;</span>         <span>&lt;hr&gt;</span>         {{ wtf.quick_form(form) }}     <span>&lt;/div&gt;</span>     <span>&lt;div</span> <span>class=</span><span>&quot;col-md-6&quot;</span><span>&gt;</span>     <span>&lt;/div&gt;</span> {% endblock %}

新建-&gt; /app/templates/admin/login.html

{% extends &quot;base.html&quot; %} {% import &quot;bootstrap/wtf.html&quot; as wtf %}   {% block page_content %}     <span>&lt;div</span> <span>class=</span><span>&quot;col-md-6&quot;</span><span>&gt;</span>         <span>&lt;h1&gt;</span>登入博客<span>&lt;/h1&gt;</span>         <span>&lt;hr&gt;</span>         {{ wtf.quick_form(form) }}     <span>&lt;/div&gt;</span>     <span>&lt;div</span> <span>class=</span><span>&quot;col-md-6&quot;</span><span>&gt;</span>     <span>&lt;/div&gt;</span> {% endblock %}

新建-&gt; /app/templates/admin/index.html

{% extends 'base.html' %} {% block page_content %}     {% if current_user.is_authenticated %}         <span>&lt;p&gt;</span>感谢登入<span>&lt;/p&gt;</span>     {% else %}         您还没有登入,请点击 <span>&lt;a</span> <span>href=</span><span>&quot;{{ url_for('admin.login') }}&quot;</span><span>&gt;</span>登入<span>&lt;/a&gt;</span>     {% endif %} {% endblock %}

新建-&gt; /app/templates/admin/article.html

新建文章时左侧为表单界面,右侧为已发表文章表格,可以在表格内点击链接删除该文章。

{% extends 'base.html' %} {% import 'bootstrap/wtf.html' as wtf %} {% block page_content %}     <span>&lt;div</span> <span>class=</span><span>&quot;col-md-6&quot;</span><span>&gt;</span>         {{ wtf.quick_form(form) }}     <span>&lt;/div&gt;</span>     <span>&lt;div</span> <span>class=</span><span>&quot;col-md-6&quot;</span><span>&gt;</span>         <span>&lt;table</span> <span>class=</span><span>&quot;table&quot;</span><span>&gt;</span>             <span>&lt;thead&gt;</span>             <span>&lt;tr&gt;</span>                 <span>&lt;th&gt;</span>文章编号<span>&lt;/th&gt;</span>                 <span>&lt;th&gt;</span>文章标题<span>&lt;/th&gt;</span>                 <span>&lt;th&gt;</span>操作<span>&lt;/th&gt;</span>             <span>&lt;/tr&gt;</span>             <span>&lt;/thead&gt;</span>             <span>&lt;tbody&gt;</span>             {% for foo in list %}                 <span>&lt;tr&gt;</span>                     <span>&lt;td&gt;</span>{{ foo.id }}<span>&lt;/td&gt;</span>                     <span>&lt;th&gt;</span>{{ foo.title }}<span>&lt;/th&gt;</span>                     <span>&lt;td&gt;&lt;a</span> <span>href=</span><span>&quot;{{ url_for('admin.article_del',id=foo.id,a='del') }}&quot;</span><span>&gt;</span>删除<span>&lt;/a&gt;&lt;/td&gt;</span>                 <span>&lt;/tr&gt;</span>             {% endfor %}             <span>&lt;/tbody&gt;</span>         <span>&lt;/table&gt;</span>     <span>&lt;/div&gt;</span> {% endblock %}

新建-&gt; /app/templates/admin/category.html

{% extends 'base.html' %} {% import 'bootstrap/wtf.html' as wtf %} {% block page_content %}     <span>&lt;div</span> <span>class=</span><span>&quot;col-md-6&quot;</span><span>&gt;</span>         {{ wtf.quick_form(form) }}     <span>&lt;/div&gt;</span>     <span>&lt;div</span> <span>class=</span><span>&quot;col-md-6&quot;</span><span>&gt;</span>         <span>&lt;table</span> <span>class=</span><span>&quot;table&quot;</span><span>&gt;</span>             <span>&lt;thead&gt;</span>             <span>&lt;tr&gt;</span>                 <span>&lt;th&gt;</span>文章编号<span>&lt;/th&gt;</span>                 <span>&lt;th&gt;</span>文章标题<span>&lt;/th&gt;</span>                 <span>&lt;th&gt;</span>操作<span>&lt;/th&gt;</span>             <span>&lt;/tr&gt;</span>             <span>&lt;/thead&gt;</span>             <span>&lt;tbody&gt;</span>             {% for foo in list %}                 <span>&lt;tr&gt;</span>                     <span>&lt;td&gt;</span>{{ foo.id }}<span>&lt;/td&gt;</span>                     <span>&lt;th&gt;</span>{{ foo.title }}<span>&lt;/th&gt;</span>                     <span>&lt;td&gt;&lt;a</span> <span>href=</span><span>&quot;{{ url_for('admin.article_del',id=foo.id,a='del') }}&quot;</span><span>&gt;</span>删除<span>&lt;/a&gt;&lt;/td&gt;</span>                 <span>&lt;/tr&gt;</span>             {% endfor %}             <span>&lt;/tbody&gt;</span>         <span>&lt;/table&gt;</span>     <span>&lt;/div&gt;</span> {% endblock %}

至此,后台功能模块全部制作完成。

如果你现在运行Flask服务器,你访问127.0.0.1:5000/admin时就可以看到一个提示,提示未登入。接下来您可以去注册,注册时需要填写正确的注册码,否则注册会不成功。

我没有描写关于修改部分,而且介于目前网络流行以markdown的格式去编写文章,在该程序中我也没有引入Markdown作为编辑器,这些都可以作为读者自己的练习题。我相信,只要细心阅读本文章及本文章代码的读者朋友们配合搜索引擎及书籍都可以自己把编辑及Markdown功能写入到后台模块里。我写本片文章是为了让大家对于Flask做博客有一定了解,其实也就是让大家对于Flask的整个项目构造方法及sql增、删、减作一个了解。真正的网站程序还有很多很多需要大家去自己去研究的地方。

在我编写的代码里,有很多是重复代码及无效代码。我希望大家在编写时能尽量优化代码结构,而不是直接复制粘贴我的代码。需要读者去阅读我的代码,我为什么这么写,那我这么写的作用在哪里。明白代码的意义之后,开始思考我用什么更好的代码来代替现有的代码行才是读者朋友们该去思考的问题。

写到这时,我依然在考虑要不要继续编写博客文章的展示部分。

我个人认为是没有必要写的,因为我在写后端模块时所使用的代码行已经完全的表达了如何从数据库读取数据及渲染到前端页面。完全可以依靠上面的代码逻辑改改参数和对象作为文章的展示逻辑代码。

最终我还是决定,写吧,反正也没多少了。

编写前台模块

前台显示文章这一块非常非常的简单。我是希望各位直接略过我这一部分,自己去思考一下该怎么写。

编辑-&gt; /app/main/views.py

<span># -*- coding=utf-8 -*-</span> <span>from</span> <span>flask</span> <span>import</span> <span>render_template</span><span>,</span> <span>redirect</span><span>,</span> <span>url_for</span><span>,</span> <span>flash</span><span>,</span> <span>request</span> <span>from</span> <span>.</span> <span>import</span> <span>main</span> <span>from</span> <span>..models</span> <span>import</span> <span>Article</span>   <span>@main.route</span><span>(</span><span>'/'</span><span>)</span> <span>def</span> <span>index</span><span>():</span>     <span>a</span> <span>=</span> <span>Article</span><span>.</span><span>query</span><span>.</span><span>all</span><span>()</span>     <span>return</span> <span>render_template</span><span>(</span><span>'index.html'</span><span>,</span> <span>list</span><span>=</span><span>a</span><span>)</span>   <span>@main.route</span><span>(</span><span>'/read/'</span><span>,</span> <span>methods</span><span>=</span><span>[</span><span>'GET'</span><span>])</span> <span>def</span> <span>read</span><span>():</span>     <span>a</span> <span>=</span> <span>Article</span><span>.</span><span>query</span><span>.</span><span>filter_by</span><span>(</span><span>id</span><span>=</span><span>request</span><span>.</span><span>args</span><span>.</span><span>get</span><span>(</span><span>'id'</span><span>))</span><span>.</span><span>first</span><span>()</span>     <span>if</span> <span>a</span> <span>is</span> <span>not</span> <span>None</span><span>:</span>         <span>return</span> <span>render_template</span><span>(</span><span>'read.html'</span><span>,</span> <span>a</span><span>=</span><span>a</span><span>)</span>     <span>flash</span><span>(</span><span>u'未找到相关文章'</span><span>)</span>     <span>return</span> <span>redirect</span><span>(</span><span>url_for</span><span>(</span><span>'main.index'</span><span>))</span>

↑创建2个路由视图,首页和详细页。

创建-&gt; /app/templates/index.html

{% extends 'base.html' %} {% block page_content %}     <span>&lt;div</span> <span>class=</span><span>&quot;col-md-8&quot;</span><span>&gt;</span>         {% for foo in list %}             <span>&lt;div</span> <span>class=</span><span>&quot;act&quot;</span><span>&gt;</span>                 <span>&lt;h3&gt;&lt;a</span> <span>href=</span><span>&quot;{{ url_for('main.read' , id=foo.id) }}&quot;</span><span>&gt;</span>{{ foo.title }}<span>&lt;/a&gt;&lt;/h3&gt;</span>             <span>&lt;/div&gt;</span>         {% endfor %}     <span>&lt;/div&gt;</span> {% endblock %}

↓在文章详细页表里,需要注意的是我是如何通过数据库关联取得到分类名的,我们的数据库里文章表对应的条目内分类只是一个ID,而我们要通过这个分类ID获取分类的name,怎么做?看下面的代码关键部分。

创建-&gt; /app/templates/read.html

{% extends 'base.html' %} {% block page_content %}     <span>&lt;div</span> <span>class=</span><span>&quot;container&quot;</span><span>&gt;</span>         <span>&lt;div</span> <span>class=</span><span>&quot;col-md-12&quot;</span><span>&gt;</span>             <span>&lt;h2&gt;</span>{{ a.title }}<span>&lt;/h2&gt;</span>             <span>&lt;hr&gt;</span>             <span>&lt;p&gt;</span>{{ a.create_time }} | {{ a.category.name }}<span>&lt;/p&gt;</span>             {{ a.body }}         <span>&lt;/div&gt;</span>     <span>&lt;/div&gt;</span> {% endblock %}

到此,一个非常简单的Blog已经编写完成。

接下来的文章里,不会再包含任何代码块的展示。

至于代码块会不会有BUG,写的好不好,在下面的测试及完善章节里讨论。

测试和完善都是什么

测试

其实Flask还有单元测试的方法,我没有去描述。因为这些不在本文的讨论范围里,我只是陈述如何去写一个Blog而已。

那么如何测试我们的写的Blog呢?运行服务器,去注册账号,写文章发布你会发现有问题,为什么?因为你没有填分类。那么分类是不是一定要写?Models.py里是怎么定义文章表里的分类字段?Flask会不会报错??这些都是要你去测试并修复的代码问题。我说过,我如果写教程一定不会写最完美的代码出来,因为这样就没有意思了,当然我认为也并不存在最完美的代码,而只有适时的代码。我只想在本文里所有的代码给与读者的感觉是比较好理解的。不写过多复杂的代码,去避免读者无谓的复制粘贴。

完善

这个Blog需要完善的地方实在是太多太多了,你可以完善Markdown编辑器、前端样式、文章编辑、分类名修改功能、引入第三方评论系统等等…………我非常欢迎如果有读者看我的文章时,一遍看一遍自己思考编写一个Blog,直至到最后发现有了问题要和我探讨,那么请给我发Mail。我会很热情的回复你的问题,共同探讨。 现在那你会说哎,作者你这个坑,你写的我都懂,那你就告诉我了现在我要去引入一个Markdown编辑器到后台,我哪里懂?其实很简单,你去搜索一下关键词,例如‘Flask-Markdown’ 、 ‘Flask使用Markdown’等等关键词找网络博主们写的文章或者问答。

不要告诉我你不会使用搜索引擎,这些问题你就算不会翻墙用百度都能解决。更何况用Google?

部署到公网服务器

部署到公网服务器的方法有很多,当然你需要一台VPS。腾讯云、阿里云各种云或者BAE,SAE这种应用商店。

我不知各位是否对Http服务器有一定了解如果不了解就去了解一下Nginx,你可以直接用Flask-script的管理启动服务器,然后用Nginx去监听本地端口映射到外网访问。也可以用wsgi 服务器网关接口 去部署Flask Web程序。

这一块,我只做提示,不做教程。各位自行Google、百度查阅方法。

扩展你的Blog

最后一章,终于快写完了。这是我第一次写这么长篇幅的文章。

自己很感谢自己能够坚持下来,也很感谢我在群内聊天时大家给出的鼓励和支持。

在最后一章,我会说的比较啰嗦。这也是整篇文章的核心所在。

前面所有的代码逻辑,我都是为了给大家心里种下一个模子,而Blog只是一个最基本的网络程序,还有很多很多需要大家去了解的地方。

那么,我们到底该如何扩展我们的Blog?

我给各位打个比喻,例如我现在想要做一个会员系统,包含更详细的会员资料,拿目前的程序来修改可不可以? 我的回答是绝对可以,我们这么考虑,详细会员资料就是一个名为 info 的表,而会员还是保持 user 表。我们去掉文章及分类表的模型,只保留 user info 并对user和info编写一对一关系,每一个info表关联一个user.id。每次查询时只需要传入user.id然后由user.id关联查询对应的info表,从而输出用户的详细资料。

这只是非常简单的一个比喻,对于数据库关联方面的扩展。

那我们继续来设想,还能怎么做?

再来一个栗子,我公司最近要求我写一个内部OA系统,要求员工可以在网站内在线请假,那么我的用户系统就是引用的上面我所说的栗子。

而用户请假这个业务该怎么去编写代码?首先我们来理解一下,在现实生活中,纸质的请假条就是作于请假的数据。那么我们要存在网上,请假条就应该是一个数据库表,而数据库表里的字段就应该是请假条需要填写的内容,例如 请假时间、回岗时间、请假类别、请假人、创建时间、假条状态、审批人、审批时间 这么些字段,而请假人及审批人的信息我们就可以关联user.id来进行关联查询,假条状态我们应该是以数字来代替,例如默认是0(待审核)、1(通过)、2(否决)。

请假类别也是数字来代替1(事假)、2(婚假)、3(工伤)、4(产假)、5(病假)等等……我们设定好了字段,在来考虑他该怎样去产生一个请假条。设想写请假条跟在后台写博客是不是一个道理呢?只是展示的位置不同而已,博客是在前台可以展示给任何人浏览,而请假条必须要有一定权限(主管或经理职位)级别的用户在后台的审批界面查看。那么我们是不是可以对我们刚才设计出来的models从而设计一个forms?

接下来在前端渲染出Form,并加上一些访问权限,有账户的才能访问,无账户的则不能访问该页面。这个业务流程就完成一半了呢?还差审批,我们知道审批肯定是有一定权限的人才可以去做到。那我们修改user表里增加一个Permission(权限)字段,并设定好字权限等级,例如1(普通)、2(主管)、3(经理)、4(总裁),2能审批1、3能审批2和1、4能审批3、2、1。在用户访问路由时直接检查用户权限,达到权限范围的才可以进行访问。是不是整套流程就清晰了。

这些就叫我所谓的扩展,因为这个Blog只是展示了最基本的sql数据的增、删过程。不同的网站只是对于数据库的字段有不同的设定,而针对你自己所需求的内容。你要最早考虑的是数据库模型,这也是为什么本文中我们在部署好Flask开发环境后,我们第一件事情就是做的思考数据库模型的原因了。

我相信,在本文发表后的不久。会有不少读者能够做出属于自己的Flask-Blog。

感谢你们的阅读,谢谢!

Millyn 2016-05-11 下午3:55 millyn.network#gmail.com 本文只授权于编程派及本人博客、微信发表。 其他网站、微信号请勿转载!

原文  http://www.codingpy.com/article/if-you-also-want-to-write-flask/
正文到此结束
Loading...