twitter-bootstrap Flask 静态文件获取 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21668742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 19:59:55  来源:igfitidea点击:

Flask static files getting 404

pythontwitter-bootstrapflask

提问by San

I am building a basic web app with following project structure. The app is fine but I am getting 404 errors for some of the static files.

我正在构建一个具有以下项目结构的基本 Web 应用程序。该应用程序很好,但我收到了一些静态文件的 404 错误。

I don't have any file like this bootstrap.css.map and not able to find enough docs related to this in flask.

我没有任何类似 bootstrap.css.map 的文件,也无法在 Flask 中找到足够多的与此相关的文档。

127.0.0.1 - - [09/Feb/2014 22:37:17] "GET /static/css/bootstrap.css.map HTTP/1.1" 404 -

@app.route('/')
def index():
print 'in /'
return send_file('templates/login.html')

Directory structure:

目录结构:

app/
├── static/
│   └── bootstrap.min.css
├── templates/
│   └── index.html
└── app.py

EDIT: Here is my login html

编辑:这是我的登录 html

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<title>API Test Application</title>

<!-- Bootstrap core CSS -->
<link href="/static/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom CSS for the 'Thumbnail Gallery' Template -->
<link href="/static/css/2-col-portfolio.css" rel="stylesheet">
</head>

<body>
 ......simple login form..........
</body>
</html>

回答by Bitmap

Set _static_folderlocation against Flask.

_static_folder针对Flask设置位置。

app = Flask(__name__)
app._static_folder = <path to to your static directory>

回答by danfromisrael

I've just had the same problem and eventually solved it like that:

我刚刚遇到了同样的问题,并最终像这样解决了它:

https://stackoverflow.com/a/29521067/303114

https://stackoverflow.com/a/29521067/303114

Edit: Main parts that i did to solve it -

编辑:我为解决它所做的主要部分 -

Project Structure:

项目结构:

enter image description here

enter image description here

server.py:

服务器.py:

from server.AppStarter import AppStarter
import os

static_folder_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "client")

app = AppStarter()
app.register_routes_to_resources(static_folder_root)
app.run(__name__)

AppStarter.py:

AppStarter.py:

from flask import Flask, send_from_directory
from flask_restful import Api, Resource
from server.ApiResources.TodoList import TodoList
from server.ApiResources.Todo import Todo


class AppStarter(Resource):
    def __init__(self):
        self._static_files_root_folder_path = ''  # Default is current folder
        self._app = Flask(__name__)  # , static_folder='client', static_url_path='')
        self._api = Api(self._app)

    def _register_static_server(self, static_files_root_folder_path):
        self._static_files_root_folder_path = static_files_root_folder_path
        self._app.add_url_rule('/<path:file_relative_path_to_root>', 'serve_page', self._serve_page, methods=['GET'])
        self._app.add_url_rule('/', 'index', self._goto_index, methods=['GET'])

    def register_routes_to_resources(self, static_files_root_folder_path):

        self._register_static_server(static_files_root_folder_path)
        self._api.add_resource(TodoList, '/todos')
        self._api.add_resource(Todo, '/todos/<todo_id>')

    def _goto_index(self):
        return self._serve_page("index.html")

    def _serve_page(self, file_relative_path_to_root):
        return send_from_directory(self._static_files_root_folder_path, file_relative_path_to_root)

    def run(self, module_name):
        if module_name == '__main__':
            self._app.run(debug=True)

i'm just trying this stuff out for the first time so you can check out my project progress on my repo in github: https://github.com/danfromisrael/TodoApp-Flask-Angular

我只是第一次尝试这些东西,所以你可以在我的 github 仓库中查看我的项目进度:https: //github.com/danfromisrael/TodoApp-Flask-Angular

回答by r3klaw

I solved mine by adding the following;

我通过添加以下内容解决了我的问题;

app._static_folder = ''

below

以下

app = Flask(__name__)

and you get

你得到

app = Flask(__name__)
app._static_folder = ''