使用Flask托管模板文件时出现问题 [英] Trouble with hosting template files using Flask
本文介绍了使用Flask托管模板文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Flask框架托管app.py中/Project/templates中的3个文件.这就是它的样子.
I want to host 3 files that are in /Project/templates in app.py using the Flask framework. Here is what it looks like.
Project
├── templates
│ ├── index.html
│ ├── index.js
| └── style.css
├── app.py
如何使用Flask框架将这些文件托管在app.py中?这是我的尝试,但这给了我空白页.
How do I host these files in app.py using the Flask framework? Here is my attempt at it, but it is giving me a blank page.
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index_html():
return render_template('index.html')
@app.route('/index.js')
def index_js():
return render_template('index.js')
@app.route('/style.css')
def style_css():
return render_template('style.css')
if __name__ == '__main__':
app.run(debug=True)
推荐答案
我相信 index.js
和 style.css
是静态文件.因此,要访问它们,请在与模板相同的目录中创建一个 static
文件夹.所以结构看起来像这样
I believe that index.js
and style.css
are static files. So to access them, create a folder static
in the same directory as templates. So the structure looks like this
Project
├── templates
│ └── index.html
├── static
│ ├── index.js
│ └── style.css
└── app.py
,然后在模板中,例如 index.html
,将其包含在
and then in the templates, for example, index.html
, include them by
<script type="text/javascript" src="{{ url_for('static', filename='index.js') }}"></script>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
这篇关于使用Flask托管模板文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文