Python烧瓶忍者图像文件未找到 [英] Python flask jinja image file not found
问题描述
我正在使用Flask,一个Python框架。 Flask使用Jinja来渲染模板。
我不知道Jinja Flask使用哪个版本,也不知道如何检索Jinja版本或Flask版本。我使用Python 2.7版。
模板在css / image目录中包含一个图像。当直接在Firefox浏览器中查看模板文件时,该图像是可见的。
但是当执行Flask时:
from flask import Flask,render_template
app = Flask(__ name__)
@ app.route('/ hello /')
@ app.route('/ hello /< name>)
def hello(name = None):
return render_template('Basic.html',name = name)
if __name__ =='__main__':
app.debug = True
app.run()
HTML文件的内容:
<!DOCTYPE HTML>
< html>
< body>
<! - 变量示例begin - >
{%if name name%}
< h1> Hello {{name}}!< / h1>
{%else%}
< h1> Hello World!< / h1>
{%endif%}
<! - 变量示例结束 - >
< img src =css / images / icons / resultset_previous.pngwidth =16height =16alt =previoustitle =Previousborder =0> ;
< / body>
< / html>
运行正常和变量示例的模板按预期方式运行。但图像不显示。
调试返回:
GET /hello/css/images/icons/resultset_previous.png HTTP / 1.1404 -
如文档中所建议的VirtualEnv。
图片的路径似乎是未知的。如何设置路径?
对于Flask,您应该将静态文件保存在一个文件夹static使用url_for函数。让我们说你的项目被称为myproject,并使用你的例子,应该看起来像这样:
myproject /
app.py
模板/
静态/
css /
图片/
图标/
resultset_previous.png
然后在模板中,您可以调用
< img src = {{url_for('static',filename ='css / images / icons / resultset_previous.png')}} width =16height =16alt =previoustitle =Previous BORDER = 0 >
另外,为了回答你关于jinja版本等的问题,请检查一下
https:// github。 com / mitsuhiko / flask / blob / master / .travis-lowest-requirements.txt
Newbie question.
I am using Flask, a webframe for Python. Flask uses Jinja to render the template. I do not know which version of Jinja Flask uses, nor do I know how to retrieve the Jinja version or the Flask version. I use Python version 2.7.
The template contains an image in the css/image directory. This image is visible when viewing the template as a file directly in the Firefox browser.
But not when executing Flask :
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('Basic.html', name=name)
if __name__ == '__main__':
app.debug = True
app.run()
The content of HTML file :
<!DOCTYPE HTML>
<html>
<body>
<!-- variable example begin -->
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello World!</h1>
{% endif %}
<!-- variable example end -->
<img src="css/images/icons/resultset_previous.png" width="16" height="16" alt="previous" title="Previous" border="0">
</body>
</html>
The template run fine and variable example runs as expected. But the images is not displayed. Debug returns : "GET /hello/css/images/icons/resultset_previous.png HTTP/1.1" 404 -
I run Flask in VirtualEnv as suggested in the documentation. The path to the image seems to be unknown. How can I set the path?
For Flask, you should keep the static files usually under a folder "static" and then use the url_for function. Lets say your project is called "myproject" and using your example, it should look something like:
myproject/
app.py
templates/
static/
css/
images/
icons/
resultset_previous.png
Then in the template, you can call
<img src= {{ url_for('static', filename = 'css/images/icons/resultset_previous.png') }} width="16" height="16" alt="previous" title="Previous" border="0">
Also, to answer your question about jinja versions etc, check this out
https://github.com/mitsuhiko/flask/blob/master/.travis-lowest-requirements.txt
这篇关于Python烧瓶忍者图像文件未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!