我正在打包我的Flask应用程序,将app.py迁移到ship_comm模块后,即使我在更改root_folder时选择了新的模板路径,也无法解析静态URL。这是我的结构:
├── 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
答案 0 :(得分:1)
root_path
应该是自动计算的。如果要设置它,请尝试使用绝对路径(假设您在app.py
中创建了应用程序:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
app = Flask(__name__, root_path=dir_path)
那么您的静态文件应该可以工作了。
答案 1 :(得分:0)
我的结构与您的结构相似,并且在尝试按照教程为小型Web应用程序创建Docker容器时遇到了此问题。我最终希望将此容器部署到QNAP,以查看是否可以自己托管东西。对我有用的是:
from flask import Flask, render_template
app = Flask(__name__, template_folder='../templates', static_folder="../static")