Python 导航中的变量传递到Jinja 2中的基本和所有模板
我有一个Python 导航中的变量传递到Jinja 2中的基本和所有模板,python,json,templates,variables,jinja2,Python,Json,Templates,Variables,Jinja2,我有一个NavHandler为nav页面传递json数据。我想包括导航到基地,使所有的网页都可以看到导航 我可以在nav.html中看到JSON数据,但它在base.html和index.html中没有显示 我可以知道如何将变量传递给所有模板吗 我错过了什么 除此之外,我还按照Jinja2示例代码测试了on@app.context_处理器和nav.html,但给出了未定义的变量 main.py from flask import Flask app = Flask(__name__) @app
NavHandler
为nav页面传递json数据。我想包括导航到基地,使所有的网页都可以看到导航
我可以在nav.html
中看到JSON数据,但它在base.html
和index.html
中没有显示
我可以知道如何将变量传递给所有模板吗
我错过了什么
除此之外,我还按照Jinja2示例代码测试了on@app.context_处理器和nav.html,但给出了未定义的变量
main.py
from flask import Flask
app = Flask(__name__)
@app.context_processor
def utility_processor():
def format_price(amount, currency=u''):
return u'{0:.2f}{1}'.format(amount, currency)
return dict(format_price=format_price)
class NavHandler(weapp2.RequestHandler):
def get(self):
json_str = '''[{"name":"Nav A", "link":"link 1"},{"name":"Nav B", "link":"link 2"},{"name":"Nav C", "link":"link 3"}]'''
json_data = json.load(json_str)
template_vars = json_data
template = JINJA_ENVIRONMENT.get.template('nav.html')
self.response.write(template.render(template_vars, jason_data=json=data))
class BaseHandler(weapp2.RequestHandler):
def get(self):
template_vars = { 'title' : title}
template = JINJA_ENVIRONMENT.get.template('nav.html')
self.response.write(template.render(template_vars))
nav.html
{% block nav %}
<ul>
{% for d from json_data %}
<li><a href="{{ d.link }}">{{ d.name }}</a>
{% endfor %}
</ul>
<script>
console.log({{ format_price(0.33) }});
</script>
{ endblock %}
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>{% block title %} - My Site</title>
</head>
<body>
<div>Navbar</div>
{% include nav.html %}
{% block content %}{% endblock %}
</body>
</html>
{% extends 'base.html' %}
{% block content %}
<h3>{% block title %}Home{% endblock %}</h3>
<p>Hello, World!</p>
{% endblock %}
{%block nav%}
{来自json_数据%d的%d}
-
{%endfor%}
log({format_price(0.33)});
{endblock%}
base.html
{% block nav %}
<ul>
{% for d from json_data %}
<li><a href="{{ d.link }}">{{ d.name }}</a>
{% endfor %}
</ul>
<script>
console.log({{ format_price(0.33) }});
</script>
{ endblock %}
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>{% block title %} - My Site</title>
</head>
<body>
<div>Navbar</div>
{% include nav.html %}
{% block content %}{% endblock %}
</body>
</html>
{% extends 'base.html' %}
{% block content %}
<h3>{% block title %}Home{% endblock %}</h3>
<p>Hello, World!</p>
{% endblock %}
{%block title%}-我的网站
导航栏
{%include nav.html%}
{%block content%}{%endblock%}
index.html
{% block nav %}
<ul>
{% for d from json_data %}
<li><a href="{{ d.link }}">{{ d.name }}</a>
{% endfor %}
</ul>
<script>
console.log({{ format_price(0.33) }});
</script>
{ endblock %}
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>{% block title %} - My Site</title>
</head>
<body>
<div>Navbar</div>
{% include nav.html %}
{% block content %}{% endblock %}
</body>
</html>
{% extends 'base.html' %}
{% block content %}
<h3>{% block title %}Home{% endblock %}</h3>
<p>Hello, World!</p>
{% endblock %}
{%extends'base.html%}
{%block content%}
{%block title%}主页{%endblock%}
你好,世界
{%endblock%}
您可以使用在模板中插入全局变量
要将新变量自动注入模板的上下文中,
Flask中存在上下文处理器。上下文处理器在
模板被呈现,并且能够将新值注入到
模板上下文
结帐,他们可能会帮助你
编辑:用于更新新问题
更改此行:
returndict(format\u price=g.format\u price)
对于这一个:
return dict(format\u price=format\u price)
所以我必须导入flask?我已经将flask安装到app lib中,然后我在这里遵循Jinja示例:当我在模板“nav.html”中尝试{format\u price(0.33)}时,它抛出了以下错误:UndefinedError:“format\u price”未定义。有什么想法吗?@dnez不看代码就没有想法,但为此,我建议您提出另一个问题,重点关注上下文处理器的范围,否则更新此问题的主体。@dnez检查答案的编辑,看看塔尔是否适合您。删除g。仍然是“未定义错误:'格式\价格'未定义'