【问题标题】:Django + React app deployed on GAE Flex but static files won't workDjango + React 应用程序部署在 GAE Flex 上,但静态文件不起作用
【发布时间】:2021-03-05 00:55:51
【问题描述】:

我有一个 Django/React 应用程序。前端由 Django 使用静态文件提供服务。因为我使用的是 GeoDjango,所以我不得不从使用 GAE 标准转向 GAE flex(使用 GDAL 构建容器,因为 GeoDjango 等需要它)。

但是,现在我的网站无法加载,并且在尝试加载静态内容时请求被取消。

我有一个app.yaml,看起来像这样:

runtime: custom
env: flex
entrypoint: gunicorn -b :$PORT appname.wsgi

runtime_config:
  python_version: 3
env_variables:
  DJANGO_SECRET_KEY: "someVariable"
  GOOGLE_API_KEY: "someVariable"
handlers:
  # This configures Google App Engine to serve the files in the app's static
  # directory.
  - url: /static
    static_dir: static/
  # This handler routes all requests not caught above to your main app. It is
  # required when static routes are defined, but can be omitted (along with
  # the entire handlers section) when there are no static files defined.
  - url: /.*
    script: auto

我的应用设置包含这样的云存储 url:

STATIC_ROOT = 'static'

STATIC_URL = '/static/'

REACT_APP_DIR = os.path.join(BASE_DIR, 'frontend')

STATICFILES_DIRS = [
    os.path.join(REACT_APP_DIR, 'build', 'static'),
]

最后但同样重要的是,这是通过 Django 呈现前端的视图:

class FrontendAppView(View):
    """
    Serves the compiled frontend entry point (only works if you have run `yarn
    run build`).
    """
    @staticmethod
    def get(request):
        print(os.path.join(settings.REACT_APP_DIR, 'build', 'index.html'))
        try:
            with open(
                    os.path.join(settings.REACT_APP_DIR, 'build',
                                 'index.html')) as f:
                return HttpResponse(f.read())
        except FileNotFoundError:
            logging.exception('Production build of app not found')
            return HttpResponse(
                """
                    This URL is only used when you have built the production
                    version of the app. Visit http://localhost:3000/ instead, or
                    run `yarn run build` to test the production version.
                    """,
                status=501,
            )

我的网站正在拨打这样的电话: http://www.domain.co.uk/static/css/main.d73ff749.chunk.css,但各种请求的状态会在几秒钟后更改为 cancelled

任何帮助将不胜感激。

【问题讨论】:

    标签: reactjs django google-app-engine google-cloud-storage infrastructure


    【解决方案1】:

    我通过使用Whitenoise解决了这个问题。

    现在网站正在按预期获取静态内容和呈现。

    【讨论】:

      猜你喜欢
      • 2013-07-27
      • 2018-02-18
      • 2018-08-07
      • 2021-01-24
      • 2019-08-19
      • 2014-06-18
      • 2019-04-04
      • 1970-01-01
      • 2017-07-16
      相关资源
      最近更新 更多