Flask无法解析静态文件 [英] Flask unable to resolve static files
问题描述
我有一个用例,我正在尝试提供一个静态页面。
这是我的app.py
from flask import Flask,request,abort,render_template
$ b $ app = Flask(__ name__)
@ app.route('/ sample / application /',methods = ['GET'])
def static_website():
返回render_template('index.html')
if __name__ =='__main__':
app .run(debug = True)
以下是我的目录布局:
flask_application /
- app.py
- static /
- index.html
- cs /
- app.css
- bootstrap.css
- js /
- jquery.js
- images /
- logo.png
在任何人将其标记为重复项之前,我已经阅读了以下文章,但尚未解决问题。 >
- 无法提供css,js和图像文件 com / questions / 31413111 / routing-static-files-using-flask>没有模板的回应+问题 再次涉及到模板
是其他问题,但没有人给出一个具体的解决方案,以解决静态页面中的CSS,JS文件。
由Flask创建的服务器无法取得js,css文件。
另外,我正在避免模板化,因为这是唯一的用例,而且我非常确定,我不会添加任何其他页面或使其更加复杂。
模板需要位于名为 templates
的文件夹中。所以正确的解决方案应该是
$ templates $ b $ index
您可以阅读 中的 I am trying to write a simple API using Flask. I am following Miguel Grinberg's blog. I have a use-case where I am trying to serve a static page. I am unable to serve the static webpage. Here is my app.py And following is my directory layout: Before anyone marks this as a duplicate, I have gone through the following posts and it has not resolved my issue. There are other questions but no ones gives a concrete solution to resolve css, js files in static pages. The server created by Flask is unable to pickup the js, css files. Also, I am avoiding templating because this the only use-case I have and I am pretty sure, I am not going to add any other pages or make it more complex. The template needs to be in a folder called You can read the documentation for the 这篇关于Flask无法解析静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! render_template方法。
from flask import Flask, request, abort, render_template
app = Flask(__name__)
@app.route('/sample/application/', methods=['GET'])
def static_website():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
flask_application/
- app.py
- static/
- index.html
- cs/
- app.css
- bootstrap.css
- js/
- jquery.js
- images/
- logo.png
templates
. So the correct solution should beflask_application/
- templates/
- index.html
render_template
method.