Flask:如何提供静态html? [英] Flask: How to serve static html?
问题描述
(editor.html的副本是在.py和模板目录)
这是我所有的尝试过:从烧瓶导入烧瓶
app =烧瓶(__ name__,static_url_path ='/模板')
@app。
route('/')
def hello_world():
#return'Hello World1!'#this works correctly!
#return render_template('editor.html')
#return render_template('/ editor.html')
#return render_template(url_for('templates',filename ='editor.html' ))
#return app.send_static_file('editor.html')#404 error(Not Found)
return send_from_directory('templates','editor.html')
$ c
$ b 这是回应:
pre $ 标题:500内部服务器错误
内部服务器错误
服务器遇到内部错误,无法完成您的请求。服务器过载或者应用程序出现错误。
减少这个工作的最简单的方法:
- 将静态资产放入您的
静态
子文件夹中。 b - 将Flask设置为默认值,不要给它一个
static_url_path
- 配置
/静态/
来验证文件是否有效
要重用一个静态文件,使用 current_app.send_static_file()
,而不要使用前面的 /
斜杠: p>
from flask import Flask,current_app $ b $ app = Flask(__ name__)
@ app.route ('/')
def hello_world():
return current_app.send_static_file('editor.html')
它在 static $ c>内直接查找文件
editor.html
$ c>文件夹。
这个假定你保存了上面的fil e在一个文件夹中有一个 static
子文件夹,其子文件夹中有一个 editor.html
b
$ b
一些进一步的注意事项:
$ ul $ b $ li static_url_path
更改 URL 静态文件可用,而不是用于从中加载数据的文件系统上的位置。
render_template / code>假定你的文件是一个Jinja2模板;如果它真的只是一个静态文件,那么这是过度杀毒,如果该文件中存在错误或缺少上下文的实际可执行语法,则 可能导致错误。
I am trying to serve a static html file, but returns a 500 error (a copy of editor.html is on .py and templates directory) This is all I have tried:
from flask import Flask
app = Flask(__name__, static_url_path='/templates')
@app.route('/')
def hello_world():
#return 'Hello World1!' #this works correctly!
#return render_template('editor.html')
#return render_template('/editor.html')
#return render_template(url_for('templates', filename='editor.html'))
#return app.send_static_file('editor.html') #404 error (Not Found)
return send_from_directory('templates', 'editor.html')
This is the response:
Title: 500 Internal Server Srror
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Reducing this to the simplest method that'll work:
- Put static assets into your
static
subfolder. - Leave Flask set to the default, don't give it a
static_url_path
either. - Access static content over the pre-configured
/static/
to verify the file works
If you then still want to reuse a static file, use current_app.send_static_file()
, and do not use leading /
slashes:
from flask import Flask, current_app
app = Flask(__name__)
@app.route('/')
def hello_world():
return current_app.send_static_file('editor.html')
This looks for the file editor.html
directly inside the static
folder.
This presumes that you saved the above file in a folder that has a static
subfolder with a file editor.html
inside that subfolder.
Some further notes:
static_url_path
changes the URL static files are available at, not the location on the filesystem used to load the data from.render_template()
assumes your file is a Jinja2 template; if it is really just a static file then that is overkill and can lead to errors if there is actual executable syntax in that file that has errors or is missing context.
这篇关于Flask:如何提供静态html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!