Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Flask Route
Search
fanker
June 24, 2014
Programming
180
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Flask Route
It talks about the route in flask
fanker
June 24, 2014
Other Decks in Programming
See All in Programming
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.4k
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
220
AmebaブログはKMPから卒業します
kmars0605
0
160
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
270
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
2
450
最近”作らせてる” ローカルAIアプリの話
akit37
0
470
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5.2k
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
420
contenteditable と日本語入力に向き合う
colorful12
0
200
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
790
APNsからLive Activityを開始する話
yumnumm
0
170
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
96
14k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
950
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
570
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.9k
Abbi's Birthday
coloredviolet
4
10k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7.1k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.7k
GraphQLとの向き合い方2022年版
quramy
50
15k
Code Review Best Practice
trishagee
74
20k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
A Tale of Four Properties
chriscoyier
163
24k
Transcript
FLASK ROUTE Author:fanker Date:2014-06-24
路由:route() 这个装饰器把一个函数绑定到对应的URL上面 eq: @app.route('/') def index(): return 'Index Page' @app.route('/hello')
def hello(): return 'Hello World'
变量规则 要给 URL 添加变量部分,你可以把这些特 殊的字段标记为 <variable_name> , 这个部分 将会作为命名参数传递到你的函数。规则 可以用
<converter:variable_name> 指定一个可选 的转换器
eq: @app.route('/user/<username>') def show_user_profile(username): #show the user profile for that
user return ‘User {0}‘.format(username) @app.route('/hello') def hello(): return 'Hello World'
唯一的网址/重定向行为 Flask的URL规则基于Werkzeug的路由模块 这个模块背后的思想是基于 Apache 以及更早的 HTTP 服务器规定的先例, 保证优雅且唯一的 URL。 eq:
@app.route('/hello/') def hello(): return ‘The Hello page' @app.route('/about') def about(): return ‘The about page'
这两种URL看起来很相似,但是: ‘/hello/’ 结尾带斜线,如果访问不带斜线的URL会被重 定向到带斜线的规范URL去 ‘/about’ 结尾不带斜线,如果访问带斜线的URL会出现 404错误
重定向函数:redirect() eq: @app.route('/') def index(): return redirect(url_for(‘login’)) 在这里当访问/的时候,会重定向到login方法
构建URL:url_for() 使用url_for()可以给一个特定的函数构造URL, 它接受一个函数名作为第一个参数和一些关 键字参数,每个对应 URL 规则的变量部分。 未知变量部分会添加到 URL 末尾作为查询参 数
HTTP请求方法 HTTP (web 应用会话的协议)知道访问 URL 的不同 方法。默认情况下,路由只回应GET 请求,但是通 过给 route()
装饰器提供 methods 参数可以更改这个 行为 @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': do_the_login() else: show_the_login_form()
静态文件 静态文件通常是CSS和JavaScript文件的存放位置 只要在包中或者模块旁边创建一个名为static的 文件夹,在应用中使用/static即可访问 给静态文件生成URL,使用特殊的’static’端点名 url_for(‘static’, filename=‘style.css’) 路径:static/style.css