css - Flask 在 aws elastic beanstalk 上将 css 文件呈现为空

标签 css flask amazon-elastic-beanstalk

我有一个 Flask 应用程序,当我通过 flask run 在本地运行它时会加载 css,但当我将它上传到 aws elastic beanstalk 时则不会。

css-test-app/ 应用程序.py 要求.txt 应用程序/ 初始化.py 路线.py 静止的/ 样式表/ 样式.css 模板/ index.html

我通过设置 export FLASK_APP=application.py 然后 flask run 在我的浏览器中转到 127.0.0.1:5000 之前将它部署到我的计算机上。

为了将它上传到 aws elastic beanstalk,我从 app/、application.py 和 requirements.txt 制作了一个 zip 文件,然后部署它,除了 css 文件外,它可以工作。当我使用开发工具检查页面时,在源代码下它显示了 static/stylesheets/style.css 中 css 文件的位置,但它是空的。我在 url MyApp-env.********.us-west-2.elasticbeanstalk.com 或 mydomain.com 上查看站点

我的 init.py 文件如下所示:

from flask import Flask
application = app = Flask(__name__)
from app import routes

app.static_folder = 'static'

application.py 看起来像这样:

from app import application

我的模板 index.html 文件如下所示:

<html>
    <head>
        <link rel="stylesheet" href="{{ url_for('static', filename='stylesheets/style.css') }}" type="text/css" />
        <title>{{ title }} - Microblog</title>
    </head>
    <body>
        <h1>Hello, {{ user.username }}!</h1>
    </body>
</html>

style.css 文件是:

body {
    color: purple;
    background-color: #d8da3d }

如果我在 chrome 中通过 ctrl -(重新加载此页面图标)或执行 ctrl f5 刷新页面,这不会改变任何内容。

如果我使用 ebcli 通过 ssh 进入 beanstalk 实例,那么我会在/opt/python/current/app/下找到该应用程序的所有文件,而 css 文件位于/opt/python/current/app/app/static/stylesheets/style.css,包含全部内容。

最佳答案

在 AWS 中,您必须将静态文件的路径设置为 app/static

在您的 aws 控制台上,转到配置 > 软件 > 修改

向下滚动到“静态文件”。

您可能会在表格的左侧看到/static/,在右侧的 Directory 下,提及 app/static/

点击保存并重新加载您的应用。

关于css - Flask 在 aws elastic beanstalk 上将 css 文件呈现为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53513396/

相关文章:

未全局使用的属性的 CSS 内联样式

html - 将不同的 CSS 文件添加到 Orchard CMS 中的不同页面?

jquery - 用户控制的 CSS 选择器是否需要清理?

python - 为什么 session cookie 在从域提供服务时有效,但在使用 IP 时无效?

amazon-web-services - AWS Beanstalk,如何自动重启(或终止)未响应的实例

amazon-ec2 - Amazon Web Services 上的 session 粘性

html - 内容太长时如何换行?

python - VS代码+ flask : ValueError: sourcecode string cannot contain null bytes

flask - 在服务器上找不到请求的 URL。如果您手动输入网址,请检查拼写并重试

node.js - 在 ElasticBeanstalk 上运行 Grunt