2011-09-22 257 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