Bootstrap Flask静态文件获取404

mrwjdhj3  于 2023-02-27  发布在  Bootstrap
关注(0)|答案(4)|浏览(425)

我正在构建一个基本的Web应用程序与以下项目结构。该应用程序是好的,但我得到404错误的一些静态文件。
我没有任何像这样的文件www.example.com,无法找到足够的文件相关的 flask 。bootstrap.css.map and not able to find enough docs related to this in 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')

目录结构:

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

编辑:这是我的登录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>
sdnqo3pr

sdnqo3pr1#

根据Flask设置_static_folder位置。

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

l7wslrjt2#

我也遇到过同样的问题,最后是这样解决的:
https://stackoverflow.com/a/29521067/303114
编辑:我解决问题的主要部分
项目结构:

server.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:

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)

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

qyyhg6bp

qyyhg6bp3#

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

app._static_folder = ''

下方

app = Flask(__name__)

你就能得到

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

2hh7jdfx4#

下方

app = Flask(__name__)

app._static_folder = 'templates/static'

(that是我的路线)

相关问题