【问题标题】:GAE Python 3: Static files are not loading on localhost but do load when deployed onlineGAE Python 3:静态文件不在本地主机上加载,但在在线部署时加载
【发布时间】:2021-01-17 17:22:59
【问题描述】:

我正在尝试将 Google App Engine Standard Flask Web 应用程序从 Python 2.7 迁移到 Python 3。部署到 apppot 时它运行良好,但我在运行本地开发服务器时仍然遇到问题。

问题是在本地开发服务器上运行时,我的应用无法找到我的静态文件。我收到此错误Uncaught SyntaxError: Unexpected token '<',它与我的 jquery-3.3.1.min.js: 文件相冲突。我有理由确定发生的事情是它试图找到我的 JQuery 文件,失败,收到 404 错误 html 页面,并抛出语法错误,因为它需要一个 JavaScript 文件而不是 HTML。

我的 JQuery 文件不是唯一受影响的静态文件。例如,我的 CSS 没有加载,我的应用找不到我的本地图片。

这里有一些代码sn-ps:

app.yaml

runtime: python38
instance_class: F4

automatic_scaling:
  max_concurrent_requests: 30

handlers:
- url: /_ah/start
  script: auto

- url: /app_shared/app_setup/app_style/*
  static_dir: app_shared/app_setup/app_style

- url: /app_shared/app_setup/app_images/*
  static_dir: app_shared/app_setup/app_images

- url: /app_shared/app_static/jquery/v_3_3_1/*
  static_dir: app_shared/app_static/jquery/v_3_3_1

- url: /app_shared/app_static/util/*
  static_dir: app_shared/app_static/util

- url: .*
  script: auto

error_handlers:
  - file: app_code/templates/errors/default.htm

  - error_code: timeout
    file: app_code/templates/errors/408_timeout.htm

h_main_header_menu.htm(在 head 标签内)

<script src="/app_shared/app_static/jquery/v_3_3_1/jquery-3.3.1.min.js"></script>

这是从项目根目录到 JQuery 文件的层次结构:app_shared/app_static/jquery/v_3_3_1_jquery-3.3.1.min.js

要运行本地开发服务器,我遵循 Google 的文档 herehere。所以我使用命令venv\Scripts\activate 后跟python main.py 运行它。

有人知道这里出了什么问题吗?我不知所措。任何帮助在这里表示赞赏。提前谢谢。

【问题讨论】:

    标签: python-3.x flask localhost google-app-engine-python static-files


    【解决方案1】:

    我想通了。我需要将我的 app_shared 文件夹重命名为静态。你可以在上面找到 Flask 文档here

    【讨论】:

      猜你喜欢
      • 2016-03-30
      • 2021-03-24
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2013-11-08
      相关资源
      最近更新 更多