有Flask的子域? [英] Subdomains with Flask?
问题描述
如果您只想处理在特定的子域下有特定的端点,您可以使用 subdomain
参数 @route
:
< code $ app $ = b
$ home $($)
返回甜蜜之家
@ app.route(/ some-route)
def some_route():
返回默认子域名(通常为www,或无保留)
@ app.route(/,subdomain =blog)
def blog_home():
返回Sweet blog
@ app.route(/格式(页面)
def blog_page(page):
return >
要在本地处理开发,您需要在 hosts
文件中创建条目以指向为您的机器分配不同的域名:
local.dev 127.0.0.1
blog.local.dev 127.0.0.1 $那么你可以使用 local.dev
和
code> blog.local.dev 而不是 localhost
来查看您的工作。
How might I go about implementing subdomains for my website with Flask? The documentation, though very good, wasn't very clear at all about this. They need not be dynamic, I am only going to be using 2 or 3 of my own choosing. How would I route them? Is it possible to test them in the normal way?(served locally by Flask)
If all you want to do is handle having particular endpoints under a particular subdomain, you can just use the subdomain
argument to @route
:
app = Flask(__name__)
@app.route("/")
def home():
return "Sweet home"
@app.route("/some-route")
def some_route():
return "on the default subdomain (generally, www, or unguarded)"
@app.route("/", subdomain="blog")
def blog_home():
return "Sweet blog"
@app.route("/<page>", subdomain="blog")
def blog_page(page):
return "can be dynamic: {}".format(page)
To handle development locally, you need to create entries in your hosts
file to point these various domains to your machine:
local.dev 127.0.0.1
blog.local.dev 127.0.0.1
Then you can use local.dev
and blog.local.dev
rather than localhost
to see your work.
这篇关于有Flask的子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!