3

我用几个应用程序(如博客、代码、帐户等)实现了简单的站点。由于体积大,我决定将一个 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
     ..........................
4

2 回答 2

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

在您的 blog/views.py 中,您可以呈现如下模板:

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

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

..等等

于 2013-04-05T19:10:38.727 回答
1

添加template_folder='templates'到每个应用程序蓝图声明:

account = Blueprint('account', __name__, template_folder='templates')

详情:https ://flask.palletsprojects.com/en/2.0.x/blueprints/#templates

于 2021-06-17T20:42:51.237 回答