用HTML上传烧瓶图片? [英] Flask image upload in HTML?
问题描述
因此,我想在我的网站(HTML)中上传图片.当我运行代码时,该框会显示出来,但是它不显示图像,而是显示图像名称.这是我使用的代码: app = Flask(名称)
So, I wanted to upload an image in my website(HTML). The box does show up when I run the code but it does not show the image, instead it shows the image name. This is the code I used: app = Flask(name)
<img src="{{ url_for('static', filename='image.jpg') }}" alt="image.jpg" width=208 length=284>
我确实尝试将图像和代码放在目录中:/html/static
I did try putting my image and code in the directory : /html/static
我什至尝试了一个像这样简单的代码,但是没有用:
I even tried a code as simple as this but it didn't work:
from flask import Flask
app = Flask(__name__)
@app.route('/new')
def new_file():
return """
<html>
<head>
</head>
<body>
<p>
This is an image.
<img src="{{ url_for('static', filename='image.jpg') }}" alt="image.jpg" width=208 length=284>
</p>
</body>
</html>"""
if __name__=='__main__':
app.run(host='0.0.0.0', debug=True)
推荐答案
我假设img标记正确位于模板中,而app = Flask(__name__)
不在模板中,您只是将它们都复制到了示例中?
I am assuming that the img tag is properly in a template and that the app = Flask(__name__)
is NOT in a template, you just copied them both into the example?
好像您在使用引号引起了问题.本质上,您不应该在双引号字符串的内部"使用双引号.这是一个带有错误的示例:"This is "the" wrong method".
这是一个没有错误的示例:"This is 'the' correct method."
注意,我已经用单引号替换了字符串中的双引号.
It looks like you have a problem with quotation marks. Essentially, you shouldn't use double-quotes "inside" a double-quote string. Here's an example with the mistake: "This is "the" wrong method".
Here's an example without the mistake: "This is 'the' correct method."
Notice I've replaced the double-quotes inside the string with single-quotes.
这是将其应用于代码的方法:
Here's how you would apply it to your code:
<img src="{{ url_for('static', filename='image.jpg') }}" alt="image.jpg" width=208 length=284>
我用单引号替换了文件名周围的双引号. 让我知道是否有帮助!
I've replaced the double quotation marks around the filename with single quotation marks. Let me know if that helps!
这篇关于用HTML上传烧瓶图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!