Skip to Content
Flask Web开发:基于Python的Web应用开发实战(第2版)
book

Flask Web开发:基于Python的Web应用开发实战(第2版)

by Miguel Grinberg
August 2018
Intermediate to advanced
230 pages
6h 42m
Chinese
O'Reilly Media, Inc. Beijing
Content preview from Flask Web开发:基于Python的Web应用开发实战(第2版)
66
7
|-requirements.txt
|-config.py
|-flasky.py
这种结构有
4
个顶级文件夹:
Flask
应用一般保存在名为
app
的包中;
和之前一样,数据库迁移脚本在
migrations
文件夹中;
单元测试在
tests
包中编写;
和之前一样,
Python
虚拟环境在
venv
文件夹中。
此外,这种结构还多了一些新文件:
requirements.txt
列出了所有依赖包,便于在其他计算机中重新生成相同的虚拟环境;
config.py
存储配置;
flasky.py
定义
Flask
应用实例,同时还有一些辅助管理应用的任务。
为了帮助你完全理解这个结构,下面几节会说明把
hello.py
应用转换成这种结构的过程。
7.2
 配置选项
应用经常需要设定多个配置。这方面最好的例子就是开发、测试和生产环境要使用不同的
数据库,这样才不会彼此影响。
除了
hello.py
中类似字典的
app.config
对象之外,还可以使用具有层次结构的配置类。
config.py
文件的内容如示例
7-2
所示,涵盖
hello.py
中的所有设置。
示例
7-2
 
config.py
:应用的配置
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python机器学习基础教程

Python机器学习基础教程

Andreas C. Müller, Sarah Guido
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
程序设计导论:Python语言实践

程序设计导论:Python语言实践

罗伯特 塞奇威克, 凯文 韦恩, 罗伯特 唐德罗

Publisher Resources

ISBN: 9787115489456