烧瓶蓝图:如何使用它们? [英] Flask Blueprints: How to use them?

查看:239
本文介绍了烧瓶蓝图:如何使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于如何在烧瓶中使用蓝图我有点困惑。首先,我的项目必须具有以下结构:

  run.py 
应用程序
- __init.py__
- admin
- - 模板
- - - - index.html
- - static
- - - img
- - - - logo.png
- - - models.py
- - views.py
- landing
- - 模板
- - - index.html
- - static
- - - img
- - - - logo.png
- - models.py
- - views。 py

我想要做的是定义两个蓝图,当我访问 / admin application / admin / templates / index.html 页面(和相应的徽标)被加载,但是当我访问 / landing application / landing / templates / index.html 页面(和相应的标志)被加载。 >

这应该不是那么困难,但由于某种原因,我感到困惑。



任何帮助表示赞赏!

编辑

更具体地说,以下使用蓝图和jinja2是否有意义?它似乎没有像预期的那样工作...
$ b $ pre $ $ $ .backstretch([
{{url_for( 'landing.static',filename ='img / bg / 01.jpg')}},
{{url_for('landing.static',filename ='img / bg / 02.jpg')} },
{{url_for('landing.static',filename ='img / bg / 03.jpg')}}
,{duration:3000,fade:750});

127.0.0.1 - - [31 / Aug / 2015 16:47:42]GET /%7B%7B%20url_for('landing.static'%20,%20filename='img/bg /01.jpg')%20%7D%7D HTTP / 1.1404 -
127.0.0.1 - - [31 / Aug / 2015 16:47:42]GET /%7B%7B%20url_for(' <%20,%20filename ='img / bg / 02.jpg')%20%7D%7D HTTP / 1.1404 -
127.0.0.1 - - [31 / Aug / 2015 16:47 :42]GET /%7B%7B%20url_for('landing.static'%20,%20filename='img/bg/03.jpg')%20%7D%7D HTTP / 1.1404 -


解决方案

以下文件应该可以帮助您达成目标:



run.py



  from application.admin import admin 
from application .landing进口登陆
$ b $从瓶子进口Flask
app = Flask(__ name__)

#注册蓝图
app.register_blueprint(admin)
app.register_blueprint(landing)

app.debug = True

print app.url_map

app.run(host ='0.0。 0.0')


$ b

application / from flask import Blueprint

landing = Blueprint('landing',__name__,template_folder ='templates',url_prefix ='/ landing',static_folder ='static')

from application.landing import views



application / admin / init strong> .py



  from flask import蓝图

admin =蓝图('admin',__name__, template_folder ='templates',url_prefix ='/ admin',static_folder ='static')
$ b $ from application.admin import views



application / landing / views.py



  from application.landing import landing 
$ b $ from flask import render_template

$ b $ landing.route('/')
def get_landing_index():
return render_template('landing /index.html')



application / admin / views .py



  from application.admin import admin 
$ b $ from flask import render_template


@ admin.route('/')
def get_admin_index():
return render_template('admin / index.html')



您应该得到以下路线:

 < ;规则'/登陆/'(HEAD,OPTIONS,GET) - > landing.get_landing_index> ;, 
< Rule'/ admin /'(HEAD,OPTIONS,GET) - > admin.get_admin_index> ;,
< Rule< / landing / static /< filename>'(HEAD,OPTIONS,GET) - > landing.static> ;,
< Rule'/ admin / static /< filename>'(HEAD,OPTIONS,GET) - > admin.static> ;,
< Rule'/ static /< filename>'(HEAD,OPTIONS,GET) - >静态>]

请注意,您将不得不更改存储模板的路径。模板位于:
$ b


  • /application/landing/templates/landing/index.html

  • /application/admin/templates/admin/index.html



重复模板文件夹中的蓝图名称可以缓解错误/功能描述此处。如果模板共享相同的名称(例如, index.html ),即使它们在不同的蓝图中,其中一个也将始终优先。更多的信息在上面给出的链接。

有关如何在与蓝图一起使用时链接模板中的静态文件的信息, / application / admin /

 <!DOCTYPE html> 
< html>
< head>
< title>管理页面< / title>
< / head>
< body>
< h1>欢迎使用管理员< / h1>
< img src ={{url_for('admin.static',filename ='cat.jpg')}}/>
< / body>
< / html>

文件 cat.jpg /application/admin/static/cat.jpg 中。对于登陆,模板 /application/landing/templates/landing/index.html 可以用同样的方法:

 <!DOCTYPE html> 
< html>
< head>
< title>登陆页面< / title>
< / head>
< body>
< h1>欢迎来到Landing< / h1>
< img src ={{url_for('landing.static',filename ='cat.jpg')}}/>
< / body>
< / html>

文件 cat.jpg /application/landing/static/cat.jpg 中。不需要重复下面的 / static / 这个蓝图名称,因为 url_for()已经把它作为参数。 p>

最后,不要忘记在需要的地方添加 __ init __。py 文件。 b

I am a bit confused on how to utilize blueprints in flask. Let me show you what I would like to do:

First of all, my project must have the following structure:

run.py
application
-- __init.py__
-- admin
-- -- templates
-- -- -- index.html
-- -- static
-- -- -- img
-- -- -- -- logo.png
-- -- models.py 
-- -- views.py 
-- landing
-- -- templates
-- -- -- index.html
-- -- static
-- -- -- img
-- -- -- -- logo.png
-- -- models.py 
-- -- views.py 

What I would like to do is define two blueprints in such a way that when I visit /admin the application/admin/templates/index.html page (and the corresponding logo) gets loaded but when I visit /landing the application/landing/templates/index.html page (and the corresponding logo) gets loaded.

This shouldn't be that difficult but I get confused for some reason.

Any help is appreciated!

EDIT

more specifically, does the following use of blueprints and jinja2 make sense? it doesn't seem to work as expected...

    $.backstretch([
        "{{ url_for('landing.static' , filename='img/bg/01.jpg') }}", 
        "{{ url_for('landing.static' , filename='img/bg/02.jpg') }}",
        "{{ url_for('landing.static' , filename='img/bg/03.jpg') }}"
    ], {duration: 3000, fade: 750});

    127.0.0.1 - - [31/Aug/2015 16:47:42] "GET /%7B%7B%20url_for('landing.static'%20,%20filename='img/bg/01.jpg')%20%7D%7D HTTP/1.1" 404 -
    127.0.0.1 - - [31/Aug/2015 16:47:42] "GET /%7B%7B%20url_for('landing.static'%20,%20filename='img/bg/02.jpg')%20%7D%7D HTTP/1.1" 404 -
    127.0.0.1 - - [31/Aug/2015 16:47:42] "GET /%7B%7B%20url_for('landing.static'%20,%20filename='img/bg/03.jpg')%20%7D%7D HTTP/1.1" 404 -

解决方案

The following files should help you achieve what you want:

run.py

from application.admin import admin
from application.landing import landing

from flask import Flask
app = Flask(__name__)

# Register the blueprints
app.register_blueprint(admin)
app.register_blueprint(landing)

app.debug = True

print app.url_map

app.run(host='0.0.0.0')

application/landing/init.py

from flask import Blueprint

landing = Blueprint('landing', __name__, template_folder='templates', url_prefix='/landing', static_folder='static')

from application.landing import views

application/admin/init.py

from flask import Blueprint

admin = Blueprint('admin', __name__, template_folder='templates', url_prefix='/admin', static_folder='static')

from application.admin import views

application/landing/views.py

from application.landing import landing

from flask import render_template


@landing.route('/')
def get_landing_index():
    return render_template('landing/index.html')

application/admin/views.py

from application.admin import admin

from flask import render_template


@admin.route('/')
def get_admin_index():
    return render_template('admin/index.html')

You should get the following routes:

<Rule '/landing/' (HEAD, OPTIONS, GET) -> landing.get_landing_index>,
<Rule '/admin/' (HEAD, OPTIONS, GET) -> admin.get_admin_index>,
<Rule '/landing/static/<filename>' (HEAD, OPTIONS, GET) -> landing.static>,
<Rule '/admin/static/<filename>' (HEAD, OPTIONS, GET) -> admin.static>,
<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>]

Note however that you would have to change the path at which you store your templates. The templates are located in:

  • /application/landing/templates/landing/index.html
  • /application/admin/templates/admin/index.html

Repeating the blueprint name in the templates folder is here to mitigate the bug/feature described here. If the templates share the same name (e.g. index.html), even though they are in different blueprints, one of them would always take precedence. More information in the link given above.

For info regarding how to link static files in templates when used together with blueprints, /application/admin/templates/admin/index.html may look like:

<!DOCTYPE html>
<html>
<head>
    <title>Admin page</title>
</head>
<body>
<h1>Welcome to Admin</h1>
<img src="{{ url_for('admin.static', filename='cat.jpg') }}"/>
</body>
</html>

The file cat.jpg would be available in /application/admin/static/cat.jpg. For landing, the template /application/landing/templates/landing/index.html could be made in the same way:

<!DOCTYPE html>
<html>
<head>
    <title>Landing page</title>
</head>
<body>
<h1>Welcome to Landing</h1>
<img src="{{ url_for('landing.static', filename='cat.jpg') }}"/>
</body>
</html>

The file cat.jpg would be stored in /application/landing/static/cat.jpg. No need to repeat the blueprint name here below /static/ as url_for() already gets it as argument.

Finally, don't forget to add __init__.py files wherever they are needed.

这篇关于烧瓶蓝图:如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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