heroku Django/Wagtail静态资产只有在登录时返回404,但工作注销时返回404

ufj5ltwl  于 2023-05-07  发布在  Go
关注(0)|答案(1)|浏览(126)

升级Wagtail和支持包后,我遇到了一个奇怪的问题,所有的静态资产(CSS,JS,字体等)在登录Wagtail管理员时返回404错误。会话ID是有效的并且在浏览器中被设置。但是,在未登录时可以访问(没有会话ID cookie)。这对于面向公众的站点以及/admin都是如此
网站正在运行:

  • Django==4.1.8
  • wagtail==4.2.2
  • 白噪声==6.4.0
  • Python==3.11.2
  • 由Heroku提供。

生产和开发(目前是本地的)都是使用STATICFILES_STORAGE = whitenoise.storage.CompressedManifestStaticFilesStorageSTATIC_URL = "static/"设置的。迁移和collectstatic也已经运行。
我已经测试了几种方法:

使用curl

无Cookie:curl -I http://127.0.0.1:8000/static/wagtailadmin/js/vendor.610e8c3f47d9.js

HTTP/1.1 200 OK
Date: Mon, 01 May 2023 06:11:55 GMT
Server: WSGIServer/0.2 CPython/3.11.2
Content-Type: text/javascript; charset="utf-8"
Cache-Control: max-age=315360000, public, immutable
Access-Control-Allow-Origin: *
Vary: Accept-Encoding, Cookie
Last-Modified: Mon, 01 May 2023 03:59:51 GMT
ETag: "644f3937-41103"
Content-Length: 266499
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
X-Frame-Options: DENY

使用Cookie:curl -I http://127.0.0.1:8000/static/wagtailadmin/js/vendor.610e8c3f47d9.js -H "Cookie: csrftoken=fPp7Oq2lR90jkilUiPDRHTOPTsu2lMZm;sessionid=his9jbjahqa3j86hjueeop0lno8pb4a9"

HTTP/1.1 404 Not Found
Date: Mon, 01 May 2023 06:15:53 GMT
Server: WSGIServer/0.2 CPython/3.11.2
Content-Type: text/html; charset=utf-8
Content-Length: 2761
Vary: Cookie

**无效的sessionid:**à la sessionid=thisisnotvalid返回资产fine

设置STATIC_URL的完整URL

  • 如果我设置STATIC_URL = "http://127.0.0.1:8000/static/"并通过http://localhost:8000登录,一切都工作得很好。如果我设置STATIC_URL = "http://localhost:8000/static/"没有骰子!换了也一样。显然STATIC_URLWAGTAILADMIN_BASE_URL不可能相同。

在生产环境中,我并没有选择(或者不想)从一个单独的域提供资源。我希望它能像正式的那样工作。

调试设置

**DEBUG=True:**设置STATIC_URLstatic/一起工作,但当它与访问域相同时不工作,当设置为备用时工作。
**DEBUG=False:**如上所述工作。static/和匹配域不起作用。另一个人知道。

关联配置位

base.py:

STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

STATICFILES_DIRS = [
    os.path.join(PROJECT_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

dev.py:

STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATIC_URL = "http://127.0.0.1:8000/static/"

production.py:

STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATIC_URL = "https://www.domainredacted.com/static/" # Formally just "static/"

现在很晚了,也许我在升级的时候错过了什么,但我似乎找不到它。任何帮助都非常感谢。

应用及中间件:

INSTALLED_APPS = [
   ......
    "generic_chooser",
    "django_extensions",
    "storages",  # For S3 Storage
    "wagtail_2fa",
    "django_otp",
    "django_otp.plugins.otp_totp",
    "captcha",
    "wagtailcaptcha",
    "wagtail.contrib.modeladmin",
    "wagtail.contrib.forms",
    "wagtail.contrib.redirects",
    "wagtail.contrib.settings",
    "wagtail.contrib.routable_page",
    "wagtailmetadata",
    "wagtail.embeds",
    "wagtail.sites",
    "wagtail.users",
    "wagtail.snippets",
    "wagtail.documents",
    "wagtail.images",
    "wagtail.search",
    "wagtail.admin",
    "wagtail",
    "wagtail.contrib.search_promotions",
    "wagtail.contrib.table_block",
    "modelcluster",
    "taggit",
    "django.contrib.sitemaps",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "whitenoise.runserver_nostatic",
    "django.contrib.staticfiles",
]

MIDDLEWARE = [
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "wagtail_2fa.middleware.VerifyUserMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",
    "wagtail.contrib.redirects.middleware.RedirectMiddleware",
]
bq3bfh9z

bq3bfh9z1#

结果是一个很小的东西。由于这个information from Rvector,我将安全和白噪声中间件移到了顶部。不知道为什么以前没有这个问题。活到老学到老!

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "wagtail_2fa.middleware.VerifyUserMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "wagtail.contrib.redirects.middleware.RedirectMiddleware",
]

相关问题