Python烧瓶忍者图像文件未找到

编程入门 行业动态 更新时间:2024-10-07 08:25:04
本文介绍了Python烧瓶忍者图像文件未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 新手问题。

我正在使用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

github/mitsuhiko/flask/blob/master/.travis-lowest-requirements.txt

更多推荐

Python烧瓶忍者图像文件未找到

本文发布于:2023-10-10 18:11:03,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:烧瓶   忍者   未找到   图像文件   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!