Flask无法解析静态文件 [英] Flask unable to resolve static files

查看:240
本文介绍了Flask无法解析静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Flask编写一个简单的API。我正在关注Miguel Grinberg的博客



我有一个用例,我正在尝试提供一个静态页面。



这是我的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文件。

由Flask创建的服务器无法取得js,css文件。

另外,我正在避免模板化,因为这是唯一的用例,而且我非常确定,我不会添加任何其他页面或使其更加复杂。

解决方案

模板需要位于名为 templates 的文件夹中。所以正确的解决方案应该是



$ templates $ b $ index

您可以阅读 中的 render_template方法。

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

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)

And following is my directory layout:

flask_application/
    - app.py
    - static/
        - index.html
        - cs/
            - app.css
            - bootstrap.css
        - js/
            - jquery.js
        - images/
            - logo.png

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 templates. So the correct solution should be

flask_application/
   - templates/
     - index.html 

You can read the documentation for the render_template method.

这篇关于Flask无法解析静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆