簡體   English   中英

Django 靜態文件在 DEBUG=True 和 `collectstatic` 之后不一致

[英]Django static files not consistent between DEBUG=True and after `collectstatic`

我的“選擇所有 {amount} {model}s”操作應該在 Django 管理員中查看模型頁面時出現在操作欄中,但在生產中不起作用。

預期的行為,如在local_settings.py中使用DEBUG=True記錄在本地運行: Django 頁腳操作欄顯示一個下拉菜單,然后是三個按鈕; “100 out of 100 selected”、“Select all 3000 Films”和“Execute”全部翻譯成荷蘭語。

暫存部署和使用DEBUG=False在本地運行時的行為: Django 頁腳操作欄顯示一個下拉菜單,然后是三個按鈕; “選擇 100 個中的 0 個”,然后稍微高一點的偏移量表示“選擇 100 個中的 100 個”和“執行”。沒有“全選”。

我在使用DEBUG=True本地運行runserver命令與在運行collectstatic然后在我的設置中使用DEBUG=False運行相同代碼庫之間運行模板正在使用的靜態文件之間存在不一致的問題。

檢查頁面時,靜態文件的差異在列出的源中也很明顯。 正常工作:

顯示文件“actions.js”的文件系統

無法正常工作:

顯示文件“actions.min.js”的文件系統

運行collectstatic命令會給我這個輸出:

Loading .env environment variables...
Found another file with the destination path 'admin/js/actions.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
Found another file with the destination path 'admin/js/admin/RelatedObjectLookups.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
Found another file with the destination path 'admin/js/admin/DateTimeShortcuts.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

但是在本地, actions.jsactions.min.js都存在於我的靜態文件夾中。 我不想在生產部署上打開調試,但目前不知道如何解決我遇到的這個問題。 所有幫助將不勝感激。

編輯:我的設置文件被請求,所以這是一個版本(清除敏感數據):

import os

DEBUG = False
MAINTENANCE_MODE = False
TEMPLATE_DEBUG = DEBUG
DEPLOYMENT_NAMESPACE = os.getenv("K8_NAMESPACE", "")
DEPLOY_LOCATION = "testing"
SESSION_COOKIE_AGE = 604800  # A week
DATABASES: dict = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "HOST": "127.0.0.1",
        "NAME": f"[DB_NAME]",
        "USER": "postgres",
        "PASSWORD": "[PASSWORD]",
    }
}
DATABASES["readonly"] = DATABASES["default"].copy()
DATABASES["readonly"]["USER"] += "_readonly"
DATABASES["readonly"]["TEST"] = {"MIRROR": "default"}

UNICORN_VERSION = os.getenv("UNICORN_VERSION", "[COMPANY_NAME]")

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = PROJECT_PATH.joinpath("static")

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = "/static/"

# Additional locations of static files
STATICFILES_DIRS = (
    PROJECT_PATH.joinpath("versions", UNICORN_VERSION, "static"),
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    "django.contrib.staticfiles.finders.FileSystemFinder",
)

LOCALE_PATHS = [PROJECT_PATH.joinpath("locale")]

# When a version defines a locale folder, it becomes the main
LOCALE_PATHS = getattr(version_settings, "LOCALE_PATHS", LOCALE_PATHS)

INSTALLED_APPS = [
    "django.contrib.contenttypes",
    "django.contrib.auth",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django_otp",
    "django_otp.plugins.otp_static",
    "django_otp.plugins.otp_totp",
    "two_factor",
    "grappelli",
    "grappelli_filters",
    "django.contrib.admin",
    "rangefilter",
    "admin_numeric_filter",
    "admin_auto_filters",
    "api",
]

這不是一個完整的答案

這可能是由於未為靜態文件創建命名空間而導致的問題。 引用 Django 的文檔:

現在我們也許可以將靜態文件直接放在my_app/static/中(而不是創建另一個my_app子目錄),但這實際上是個壞主意。 Django 將使用它找到的第一個名稱匹配的靜態文件,如果您在不同的應用程序中有一個同名的靜態文件,Django 將無法區分它們。 我們需要能夠將 Django 指向正確的位置,而確保這一點的最佳方法是對它們進行命名空間。 也就是說,將這些靜態文件放在另一個以應用程序本身命名的目錄中。

您可以通過指定前綴來命名STATICFILES_DIRS中的靜態資產。

如果您創建了一些自定義 JavaScript 或 CSS,請確保將它們放在具有不同名稱的static文件夾內的文件夾中。 這樣文件路徑將是唯一的,並且文件將被收集。

更新 1

在深入了解 Django 管理員和 Grappelli 的靜態文件后,我在 Grappelli 的DateTimeShortcuts.js中發現了一些有趣的東西。 這個文件的內容是:

// dropped
// not used in grappelli
// kept this file to prevent 404

grappelli優先於django.contrib.admin時,此文件將覆蓋 Django 管理員提供的原始DateTimeShortcuts.js文件。 除了警告指定的文件之外,文件之間可能存在一些差異,這可能會導致這種不需要的行為。 靜態文件問題是由grappelli包引起的。

我通過將我的 grappelli 版本從 2.15 更新到 3.0 解決了這個問題。 我懷疑這個問題與我遇到的問題類似,升級版本似乎已經解決了。

問題中最明顯的部分是縮小的actions.min.js不斷在我的靜態文件中彈出,這似乎不是actions.js文件的grappelli 版本,而是“香草” Django 文件,解釋了缺少“全選”功能。 我懷疑我的 Django 版本和 Grappelli 的組合導致這個問題像以前一樣煩人。 兩者的版本更新似乎是最可靠的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM