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版)
用户身份验证
81
app/auth/views.py
模块导入蓝本,然后使用蓝本的 route 装饰器定义与身份验证相关的路
由,如示例
8-4
所示。这段代码添加了一个
/login
路由,渲染同名占位模板。
示例
8-4
 
app/auth/views.py
:身份验证蓝本中的路由和视图函数
from flask import render_template
from . import auth
@auth.route('/login')
def login():
return render_template('auth/login.html')
注意,为
render_template()
指定的模板文件保存在
auth
目录中。这个目录必须在
app/
templates
中创建,因为
Flask
期望模板的路径是相对于应用的模板目录而言的。把蓝本中
用到的模板放在单独的子目录中,能避免与
main
蓝本或以后添加的蓝本发生冲突。
我们也可以配置蓝本使用专门的目录保存模板。如果配置了多个模板目录,
那么
render_template()
函数会先搜索应用的模板目录,然后再搜索蓝本的
模板目录。
auth 蓝本要在 create_app() 工厂函数中附加到应用上,如示例
8-5
所示。
示例
8-5
 
app/__init__.py
:注册身份验证蓝本
def create_app(config_name):
# ...
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint, ...
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