Flask在移动app.py和static文件夹后无法解析静态文件 [英] Flask unable to resolve static files after moving app.py and static folder

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

问题描述

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

I am packaging up my Flask app and I am unable to resolve static URLs after migrating the app.py into the module ship_comm even though it is picking up the new templates path when I change the root_folder. Here is my structure:

├── 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应用程序的初始化

And here is my initialization of the Flask app

app = Flask(__ name__,root_path ='ship_comm')

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

This fixed my issue with TemplateNotFound error, but still it's not able to find the static folder, even though it should be able to find it in the root_path right? I have tried explicitly stating static_folder='ship_comm/static' and static_folder='static' but that also didn't help.

我使用 python -m ship_comm.app

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

Here is my layout.html that is now missing the logo:

<!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

I am using Python 3.6.7 and Flask==1.0.2

推荐答案

root_path 应该是自动计算的.如果要设置它,请尝试使用绝对路径(假设您在 app.py :

root_path should be automaticaly calculated. If you want to set it, try to use absolute path (assuming you create the app in app.py:

import os

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

那么您的静态文件应该可以工作了.

Then your static files should be working.

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

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