2013-04-05 62 views
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 
    .......................... 
+3

就像我瞭解你從哪裏來的:保持簡單我仍強烈建議給藍圖一個嘗試。他們專門爲這種事情設計(模塊化)。除了簡單性之外,還有其他一個原因是你沒有使用它們嗎? – 2013-04-05 10:54:28

+0

你能介紹一下如何使用藍圖嗎?我只是試着用2個應用程序來做 - 「博客」和「mysite」。現在,所有用於頂級url(localhost:5000 /)的模板都是從'blog'模板文件夾中獲取的,而不是從'mysite/template'中獲取的。 – 2013-04-05 11:14:55

+0

我使用以下藍圖註冊: 'app.register_blueprint(mysite.application,url_prefix ='/')' 'app.register_blueprint(blog.application,url_prefix ='/ blog')' – 2013-04-05 11:17:16

回答

3

尋找模板,比方說你有2個藍圖博客和帳戶。您可以博客的劃分您的個性化應用程序(藍圖)和帳戶如下:

myproject/ 
    __init__.py 
    templates/ 
     base.html 
     404.html 
     blog/ 
      template.html 
      index.html 
      post.html 
     account/ 
      index.html 
      account1.html 
    blog/ 
     __init__.py 
     views.py 
    account/ 
     __init__.py 
     views.py 

在你的博客/ views.py,可以渲染模板,如:

@blog.route('/') 
def blog_index(): 
    return render_template('blog/index.html') 

@account.route('/') 
def account_index(): 
    return render_template('account/index.html') 

..和等

+3

它是有道理的。但我不喜歡它。由於某些原因,我更願意將應用程序範圍內的所有資源保留在應用程序目錄中 – 2013-04-07 09:16:24

相關問題