楼主: jieforest

利用Python Flask构建Web网站

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
11#
 楼主| 发表于 2012-10-16 13:01 | 只看该作者
File organization and management script

In our example, our application is a single file. As you can guess, this will not suffice as it becomes larger.
I find a good approach is to treat the app as a python module. The module name will be the app name and the initialization of the Flask object is done in the __init__.py file.

The templates/ folder will be located inside the module’s directory as well as any other files related to our application (eg. settings.py, models.py…).
  1. example/
  2.   __init__.py
  3.   static/
  4.     favicon.ico
  5.   templates/
  6.     index.html
  7.     hello.html
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
12#
 楼主| 发表于 2012-10-16 13:02 | 只看该作者
Note: as the application grows larger, you should use Flask’s Blueprints to organize your code as modules. I will cover that in another tutorial.

I also like to use the Flask-Script extension to manage my application from the command line. This extension provides built-in commands as well as a mechanism to define your own ones.
  1. $ pip install Flask-Assets
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
13#
 楼主| 发表于 2012-10-16 13:02 | 只看该作者
I configure and run the extension from a manage.py file located outside the module’s directory:
  1. #!/usr/bin/env python
  2. from flaskext.script import Manager, Shell, Server
  3. from example import app

  4. manager = Manager(app)
  5. manager.add_command("runserver", Server())
  6. manager.add_command("shell", Shell())
  7. manager.run()
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
14#
 楼主| 发表于 2012-10-16 13:02 | 只看该作者
To start the server from the command line:
  1. $ ./manage.py runserver
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
15#
 楼主| 发表于 2012-10-16 13:03 | 只看该作者
Using a database

There is no out of the box support for any database with Flask. Which is not a problem as there are many database libraries in Python. The most known and the one I prefer is SqlAlchemy. In addition to its excellent database toolkit, it features an awesome ORM.

Integrating SqlAlchemy with Flask cannot be simpler, thanks to the Flask-SqlAlchemy extension.
  1. $ pip install Flask-SqlAlchemy
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
16#
 楼主| 发表于 2012-10-18 12:13 | 只看该作者
本帖最后由 jieforest 于 2012-10-18 12:14 编辑

If it’s your first time with SqlAlchemy, I would advice reading the ORM tutorial which is a very good start.

I like to initialize the extension and configure my models in a models.py file.

  1. from flask_sqlalchemy import SQLAlchemy
  2. from example import app

  3. db = SQLAlchemy(app)

  4. class User(db.Model):
  5.     id = db.Column(db.Integer, primary_key=True)
  6.     username = db.Column(db.String)
  7.     message = db.Column(db.String)

  8.     def __init__(self, username, message):
  9.         self.username = username
  10.         self.message = message
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
17#
 楼主| 发表于 2012-10-18 12:14 | 只看该作者
We’ll then need to add the database connection parameters to the configuration. For this example, we’ll usesqlite. In __init__.py add this line after the previous app.config one:
  1. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://example.db'
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
18#
 楼主| 发表于 2012-10-18 12:15 | 只看该作者
We can now modify our example to use the model, in __init__.py:
  1. from models import *

  2. # ...

  3. @app.route('/signup', methods=['POST'])
  4. def signup():
  5.     user = User(request.form['username'], request.form['message'])
  6.     db.session.add(user)
  7.     db.session.commit()
  8.     return redirect(url_for('message', username=user.username))

  9. @app.route('/message/<username>')
  10. def message(username):
  11.     user = User.query.filter_by(username=username).first_or_404()
  12.     return render_template('message.html', username=user.username,
  13.                                            message=user.message)
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
19#
 楼主| 发表于 2012-10-18 12:16 | 只看该作者
Instead of using the session we are now creating a User object, storing it into the database usingdb.session.add() and db.session.commit() (which is the standard SqlAlchemy way of doing it).

In the message() function I have added a username parameter which must be given through the url’s path. We next perform a database query using User.query. Note the first_or_404() function which is provided by the flask extension and is a nice addition.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
20#
 楼主| 发表于 2012-10-18 12:20 | 只看该作者
Configuration

As we’ve seen in the previous example, the configuration can be defined using the app.config dict. While this is the easier method, it is not the most practical one once you factor in configuration environments. Indeed, the configuration of your app will most of the time be different between your development environment and your production one.

The Flask documentation suggests a nice way of handling these configuration environments.

We’ll store our configuration as attributes of plain old python objects in a file called settings.py. After reading the current environment form a system environment variable (EXAMPLE_ENV in our case) we’ll load the config from the correct config object.

In the settings.py file as follow:
  1. class Config(object):
  2.     SECRET_KEY = 'secret key'

  3. class ProdConfig(Config):
  4.     SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/example'

  5. class DevConfig(Config):
  6.     DEBUG = True
  7.     SQLALCHEMY_DATABASE_URI = 'sqlite://example.db'
  8.     SQLALCHEMY_ECHO = True
复制代码

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表