2
我實現簡單的網站與幾個應用程序(如博客,代碼,帳戶等)。由於尺寸較大,我決定將一個python文件分割爲多個應用程序。除了Flask的基本功能之外,我不使用藍圖或其他東西 - 我希望儘可能簡單。 不幸的是,瓶中還處於Flask每個應用程序模板文件夾
/site
|-> main.py
from flask import Flask
app = Flask(__name__)
app.config.from_pyfile('config.py')
# Import all views
from errors.views import * # Errors hasn't its specific prefix
from blog.views import *
from account.views import *
from mysite.views import *
if __name__ == "__main__":
app.run(debug=True)
|-> templates
...................
|->blog
|-> template
|-> _layout.html
|-> index.html
|-> post.html
|-> __init__.py
from main import app
import blog.views
|-> views
from blog import app
from flask import render_template
@app.route("/blog/", defaults={'post_id': None})
@app.route("/blog/<int:post_id>")
def blog_view(post_id):
if post_id:
return "Someday beautiful post will be here with id=%s" % post_id
else:
return "Someday beautiful blog will be here"
@app.route("/blog/tags/")
def tags_view():
pass
..........................
就像我瞭解你從哪裏來的:保持簡單我仍強烈建議給藍圖一個嘗試。他們專門爲這種事情設計(模塊化)。除了簡單性之外,還有其他一個原因是你沒有使用它們嗎? – 2013-04-05 10:54:28
你能介紹一下如何使用藍圖嗎?我只是試着用2個應用程序來做 - 「博客」和「mysite」。現在,所有用於頂級url(localhost:5000 /)的模板都是從'blog'模板文件夾中獲取的,而不是從'mysite/template'中獲取的。 – 2013-04-05 11:14:55
我使用以下藍圖註冊: 'app.register_blueprint(mysite.application,url_prefix ='/')' 'app.register_blueprint(blog.application,url_prefix ='/ blog')' – 2013-04-05 11:17:16