Reputation: 91
My "Select all {amount} {model}s" action, which should be in the actions bar while viewing a model page in the Django admin, does not work in production.
Expected behaviour, as recorded running locally with DEBUG=True
in local_settings.py
:
Behaviour on staging deployment and while running locally with DEBUG=False
:
I'm having issues with inconsistency between the static files my templates are working with between a local running of the runserver
command with DEBUG=True
vs. running the same codebase after running collectstatic
and then running it with DEBUG=False
in my settings.
The differences in static files is also apparent in the listed sources when inspecting the page. Working correctly:
Not working correctly:
Running the collectstatic
command gives me this output:
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.
But locally, both actions.js
and actions.min.js
are present in my static files folder. I don't want to turn on DEBUG on the production deployments, but currently don't know how to solve this issue I'm having. All help would be appreciated.
EDIT: My settings file was requested, so here's a version (cleaned of sensitive data):
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",
]
Upvotes: 2
Views: 776
Reputation: 91
I have solved the problem by updating my grappelli version from 2.15 to 3.0. I suspect this issue is similar to the problem I encountered, and upgrading versions seems to have solved it.
The most visible part of the problem was the fact that a minified actions.min.js
kept popping up in my static files, which seemed to not be the grappelli version of the actions.js
file but the "vanilla" Django one, explaining the lack of the "Select all" functionality. I suspect a combination of my Django version and Grappelli caused this issue to be as annoying as it was. Version updates to both seem to be the most reliable solution.
Upvotes: 0
Reputation: 136
This is not a full answer
This is likely an issue caused by not creating namespaces for the static files. Quoting the Django's documentation:
Now we might be able to get away with putting our static files directly in
my_app/static/
(rather than creating anothermy_app
subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the best way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.
You can namespace static assets in
STATICFILES_DIRS
by specifying prefixes.
If you created some custom JavaScript or CSS make sure you put them in a folder inside static
folder with a different name. This way the file path will be unique and the file will be collected.
UPDATE 1
After diving into the static files of both Django's admin and Grappelli I found something interesting in Grappelli's DateTimeShortcuts.js
. The content of this file is:
// dropped
// not used in grappelli
// kept this file to prevent 404
When prioritizing grappelli
over django.contrib.admin
this file overrides the original DateTimeShortcuts.js
file provided by Django's admin. And there might be some differences between the files other than those specified by the warning which might cause such an unwanted behavior. The static files issue is caused by grappelli
package.
Upvotes: 3