有Flask的子域? [英] Subdomains with Flask?

查看:178
本文介绍了有Flask的子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以用Flask来为我的网站实现子域名?文件虽然非常好,但对此并不十分清楚。他们不需要动态的,我只会使用我自己选择的2或3。我将如何路由他们?是否有可能以正常的方式进行测试?(由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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆