Flask 简明教程
Flask – Extensions
Flask 通常被称为微框架,因为它包括的核心的功能是基于 Werkzeug 的 WSGI 和路由,以及基于 Jinja2 的模板引擎。此外,Flask 框架支持 cookie 和会话,以及像 JSON 这样的 Web 帮助器、静态文件等。显然,这不足以开发一个功能齐全的 Web 应用程序。这意味着 Flask 扩展出现在了眼前。Flask 扩展为 Flask 框架提供可扩展性。
Flask is often referred to as a micro framework, because a core functionality includes WSGI and routing based on Werkzeug and template engine based on Jinja2. In addition, Flask framework has support for cookie and sessions as well as web helpers like JSON, static files etc. Obviously, this is not enough for the development of a full-fledged web application. This is where the Flask extensions come in picture. Flask extensions give extensibility to Flask framework.
大量的 Flask 扩展可用。Flask 扩展是一个 Python 模块,它为 Flask 应用程序添加了特定类型支持。Flask 扩展注册表是一个可用的扩展目录。必需的扩展可以通过 pip 实用程序下载。
There are a large number of Flask extensions available. A Flask extension is a Python module, which adds specific type of support to the Flask application. Flask Extension Registry is a directory of extensions available. The required extension can be downloaded by pip utility.
在本教程中,我们将讨论以下重要的 Flask 扩展 −
In this tutorial, we will discuss the following important Flask extensions −
-
Flask Mail − provides SMTP interface to Flask application
-
Flask WTF − adds rendering and validation of WTForms
-
Flask SQLAlchemy − adds SQLAlchemy support to Flask application
-
Flask Sijax − Interface for Sijax - Python/jQuery library that makes AJAX easy to use in web applications
每种类型的扩展通常会提供关于其使用情况的详尽文档。由于扩展是一个 Python 模块,所以需要导入它才能使用它。Flask 扩展通常被命名为 flask-foo。要导入,
Each type of extension usually provides extensive documentation about its usage. Since an extension is a Python module, it needs to be imported for it to be used. Flask extensions are generally named as flask-foo. To import,
from flask_foo import [class, function]
对于 Flask 0.7 之后的版本,您也可以使用语法 −
For versions of Flask later than 0.7, you can also use the syntax −
from flask.ext import foo
对于此用法,需要激活一个兼容性模块。可以通过运行 flaskext_compat.py 来安装它
For this usage, a compatibility module needs to be activated. It can be installed by running flaskext_compat.py
import flaskext_compat
flaskext_compat.activate()
from flask.ext import foo