首页 > 解决方案 > 移动 app.py 和静态文件夹后,Flask 无法解析静态文件

问题描述

我正在打包我的 Flask 应用程序,在将 app.py 迁移到模块 ship_comm 后,我无法解析静态 URL,即使它在我更改 root_folder 时正在选择新的模板路径。这是我的结构:

├── README.md 
├── requirements.txt 
├── setup.py 
└── ship_comm
    ├── app.py
    ├── config.py
    ├── database.py
    ├── db_models.py
    ├── devices.py
    ├── replay.py
    ├── router.py
    ├── routing.db
    ├── static
    │   ├── css
    │   │   └── main.css
    │   └── img
    │       ├── captain-ai-logo-small.png
    │       ├── captain-ai-logo-square.png
    │       ├── favicon.ico
    │       └── screenshot.png
    └── templates
        ├── devices.html
        ├── home.html
        ├── layout.html
        ├── messages.html
        ├── new_route.html
        ├── new_serial.html
        ├── new_udp.html
        └── routes.html

这是我对 Flask 应用程序的初始化

app = Flask(__name__, root_path='ship_comm')

这解决了我的 TemplateNotFound 错误问题,但它仍然无法找到静态文件夹,即使它应该能够在 root_path 中找到它,对吗?我曾尝试明确说明static_folder='ship_comm/static'static_folder='static'但这也无济于事。

我通过使用调用应用程序python -m ship_comm.app

这是我现在缺少徽标的 layout.html:

<!doctype html  lang="en">
<link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.ico') }}">
<title>Ship Comm Router</title>
<head>
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> -->

<!--- mobile stuff --->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8"> 
</head>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
<div class="container-fluid">
<a href="/"><h1>Ship Comm Router</h1></a>
{%- for category, message in get_flashed_messages(with_categories=true) %}
  <p class="flask {{ category }}-flash">{{
    "Error: " if category == 'error' }}{{ message }}</p>
{%- endfor %}
<hr>
{% block body %}{% endblock %}
<hr>
<div id="footer">
    <!--<img width="50px" src="../static/img/captain-ai-logo-small.png"/>-->
    <img width="50px" src="{{url_for('static', filename='img/captain-ai-logo-small.png')}}"/>
    Developed by Captain AI - 2018
</div>
</div>
<!-- <address>Flask and SQLAlchemy powered Serial and UDP Router</address> -->

我正在使用 Python 3.6.7 和 Flask==1.0.2

标签: pythonhtmlflask

解决方案


root_path应该是自动计算的。如果要设置它,请尝试使用绝对路径(假设您在以下位置创建应用程序app.py

import os

dir_path = os.path.dirname(os.path.realpath(__file__))
app = Flask(__name__, root_path=dir_path)

然后你的静态文件应该可以工作了。


推荐阅读