使用Flask/蓝图处理某些静态页面 [英] Using flask/blueprint for some static pages

查看:66
本文介绍了使用Flask/蓝图处理某些静态页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对如何使用flask构建页面而不必陈述每个视图感到有些困惑.

So I am just a little confused on how to build pages with flask without having to state each view.

我如何制作蓝图以在要加载的页面上拾取?

How would I make a blue print that would pickup on the pages i want to load?

说这些是我的示例页面

templates/
   layout.html
   section1/
     subsection/index.html
     subsection2/index.html
   section2
     subsection/index.html
       childofsubsection/index.html

如果我去了example.com/section1/subsection/,我想假设它会知道寻找其相应的页面而无需特别说明.文档 http://flask.pocoo.org/docs/blueprints/非常接近解释这一点,但我仍然有些迷茫.

I would liked to assume if i went to example.com/section1/subsection/ it would know to look for its corresponding page without having to specifically stating it. The documentation http://flask.pocoo.org/docs/blueprints/ gets very close to explaining this, but i am still a little lost.

from flask import Flask
from yourapplication.simple_page import simple_page

app = Flask(__name__)
app.register_blueprint(simple_page)

还不确定这应该去哪里?看起来就像在application.py中一样,但是要求从"yourapplication"中导入

also, not sure where this was supposed to go? this looks like it would go in the application.py, but asks to import from "yourapplication"

flask的新手,也不是python专家.真的只是需要一些愚蠢的东西:)

Very new to flask and not a python expert either. Really just need some dumbing down :)

推荐答案

如果您想查看 Blueprint 用法的示例,可以查看

If you'd like to see an example of Blueprint usage, you can have a look at this answer.

关于问题的模板自动查找"部分:如文档所述,蓝图允许指定一个文件夹,在该文件夹中将查找静态文件和/或模板,这样您就不必指定完整的您的 render_template()调用中模板文件的路径,但只有文件名.

About the "template auto-find" part of your question: like the documentation explains, blueprints allows to specify a folder where static files and/or templates will be looked for, this way you don't have to specify the full path to the template file in your render_template() call, but only the file name.

如果您希望视图神奇地"知道它们应该选择哪个文件,则必须做一些修改.例如,一种解决方案是在视图上应用装饰器,以使其根据函数名称选择模板文件,这样的装饰器将如下所示:

If you wanted your views to "magically" know which file they should pick, you have to do a bit of hack. A solution, for example, could be to apply a decorator on your view that would make it pick the template file based on the function name, such a decorator would look like this:

from functools import wraps
from flask import render_template

def autorender(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        context = func(*args, **kwargs)
        return render_template('%s.html' % func.func_name, **context)
    return wrapper

然后,您只需要将视图中的上下文作为dict返回(如果没有上下文,则返回一个空dict):

Then you would just have to return the context in your view as a dict (or an empty dict if there is no context):

@my_blueprint.route('/')
@autorender
def index():
    return {'name': 'John'} # or whatever your context is

它会自动选择名为 index.html 的模板.

And it would automatically pick the template named index.html.

这篇关于使用Flask/蓝图处理某些静态页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆