Python烧瓶模板更新
我有一个非常基本的问题(第一次使用Flask时,我不习惯web框架) 我只想更新模板中的变量,例如hello.html,其他什么都不做(我现在不关心路由或任何事情) 我是根据一个教程来做的:Python烧瓶模板更新,python,flask,Python,Flask,我有一个非常基本的问题(第一次使用Flask时,我不习惯web框架) 我只想更新模板中的变量,例如hello.html,其他什么都不做(我现在不关心路由或任何事情) 我是根据一个教程来做的: from flask import Flask, render_template app = Flask(__name__) x=1986 res=render_template('hello.html', myVar = x) print(res) if __name__ == '__main_
from flask import Flask, render_template
app = Flask(__name__)
x=1986
res=render_template('hello.html', myVar = x)
print(res)
if __name__ == '__main__':
app.run(debug = True)
请注意,我在名为templates的子目录中有一个html文件名hello.html。
hello.html:
<!doctype html>
<html>
<body>
<h1>Hello {% print(myVar) %}</h1>
</body>
</html>
您好{%print(myVar)%}
我做错了什么
编辑。我收到以下错误消息:
Traceback (most recent call last):
File "flask_test1.py", line 7, in <module>
res=render_template('hello.html', myVar = x)
File "/root/miniconda3/lib/python3.4/site-packages/flask/templating.py", line 133, in render_template
ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'
回溯(最近一次呼叫最后一次):
文件“flask_test1.py”,第7行,在
res=render_模板('hello.html',myVar=x)
文件“/root/miniconda3/lib/python3.4/site packages/flask/templating.py”,第133行,在render_模板中
ctx.app.update\u模板\u上下文(上下文)
AttributeError:“非类型”对象没有属性“应用”
顺便说一句,很抱歉我对web框架的“笨拙”…你没有说你在学习什么教程,但我很确定它没有显示那种结构 你需要把你的代码放在一个函数中,并用你想要使用的URL修饰它;然后需要从函数返回渲染模板,而不是打印它
@app.route("/")
def index():
x = 1986
res = render_template('hello.html', myVar=x)
return res
此外,在模板中不使用print()
;要输出变量的值,请使用{{}
而不是{%}
<h1>Hello {{ myVar }}</h1>
Hello{{myVar}
你没有说你在学习什么教程,但我很确定它没有显示那种结构
你需要把你的代码放在一个函数中,并用你想要使用的URL修饰它;然后需要从函数返回渲染模板,而不是打印它
@app.route("/")
def index():
x = 1986
res = render_template('hello.html', myVar=x)
return res
此外,在模板中不使用print()
;要输出变量的值,请使用{{}
而不是{%}
<h1>Hello {{ myVar }}</h1>
Hello{{myVar}
您的代码结构不可能正常工作。flask
中的基本概念是上下文。通常,您可以认为这是单个请求的完整范围
因此,正如您在错误消息中所看到的,ctx
,上下文是None
。不能像这样使用render\u模板
相反,您可以直接使用由flask
:jinja2
使用的底层渲染引擎
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
loader=FileSystemLoader('your/templates/dir'),
autoescape=select_autoescape(['html', 'xml'])
)
template = env.get_template('hello.html')
x = 1986
print(template.render(myVar = x))
您的模板应该是:
<!doctype html>
<html>
<body>
<h1>Hello {{ myVar }}</h1>
</body>
</html>
你好{{myVar}}
您的代码结构不可能正常工作。flask
中的基本概念是上下文。通常,您可以认为这是单个请求的完整范围
因此,正如您在错误消息中所看到的,ctx
,上下文是None
。不能像这样使用render\u模板
相反,您可以直接使用由flask
:jinja2
使用的底层渲染引擎
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
loader=FileSystemLoader('your/templates/dir'),
autoescape=select_autoescape(['html', 'xml'])
)
template = env.get_template('hello.html')
x = 1986
print(template.render(myVar = x))
您的模板应该是:
<!doctype html>
<html>
<body>
<h1>Hello {{ myVar }}</h1>
</body>
</html>
你好{{myVar}}
谢谢,是的,确实有这种结构(您在教程中给出)。它运行没有错误,但是我想要的只是从我的代码中获取实际的文本内容,即res,比如resourcside=index(),然后打印(resourcside),但这是失败的,是的,确实存在这种结构(您在教程中给出)。它运行没有错误,但是我想要的只是从我的代码中获取实际的文本内容,即res,比如resourceside=index(),然后打印(resourceside),但这失败了,谢谢!这正是我要找的!你真的为我节省了一些时间。哇,答案太棒了。为此,我花了两天时间。非常感谢!这正是我要找的!你真的为我节省了一些时间。哇,答案太棒了。为此我花了两天时间。非常感谢