2011-09-22 161 views
10

我使用kamalgill的flask-appengine-template作为创建我的个人网站的起点。但是,我希望将我的顶级域名作为与我的网站的不同部分(应用程序)对应的各个子域的门户。瓶子子域路由?

例如,www.spam.com应该路由到welcome.html模板。但是,eggs.spam.com应该路由到该网站的“蛋”子部分或应用程序。我会如何在烧瓶中实现这一点?

回答

25

根据您的网站将多么复杂,你可以通过你URL registration添加子域名:

from flask import Flask 

app = Flask(__name__) 

@app.route("/") 
def index(): 
    return "This is the index" 

@app.route("/", subdomain="eggs") 
def egg_index(): 
    return "You have eggs" 

或者使用瓶的Blueprint模块(api docs here)。

在eggs.py:

eggs = Blueprint("eggs", __name__, subdomain="eggs") 

# Then you can register URLs here 
@eggs.route("/") 
def index(): 
    "You have eggs" 

然后,在主routes.py:

from eggs import eggs 
from flask import Flask 

app = Flask(__name__) 

app.register_blueprint(eggs) 

@app.route("/") 
def index(): 
    return "This is the index" 

记住,所有的瓶路线是真正的werkzeug.routing.Rule实例。咨询Werkzeug's documentation for Rule将向你显示许多路线可以做的事情,Flask的文档会被掩盖(因为它已经被Werkzeug很好地记录)。

+9

记住要在瓶中的配置添加SERVER_NAME启用子域名支持http://flask.pocoo.org/docs/config/ –

+2

+1鸡蛋 –

+3

@Sean可以Desmond的注释添加到您的答案。在找到解决方法之前,我迷了好几个小时。 'app.config ['SERVER_NAME'] ='example.com:5000' – cbron