How to effectively apply custom "css" and "js" files to all admins in an application?
P粉396248578
P粉396248578 2024-04-03 20:54:06
0
1
575

I have custom css and js files, admin.py and overridden base.html located in templates/admin/app1/, as shown below:

django-project
 |-core
 |  |-settings.py
 |  └-static
 |     └-core
 |        └-admin
 |           └-app1
 |              |-css
 |              |  └-custom.css # Here
 |              └-js
 |                 └-custom.js # Here
 |-app1
 |  |-models.py
 |  └-admin.py # Here
 |-app2
 └-templates
    └-admin
       └-app1
          └-base.html # Here

First, I set the custom css and js files to all admins Person, Animal## in c respectively css and endcphpcn phpcnapp1 in the Media class of Food as shown below, I can then apply them to all admins Person, Animal and Food app1:

# "app1/admin.py"

from django.contrib import admin
from .models import Person, Animal, Food

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    class Media:
        css = {
            'all': ('core/admin/app1/css/custom.css',) # Here
        }
        js = ('core/admin/app1/js/custom.js',) # Here

@admin.register(Animal)
class AnimalAdmin(admin.ModelAdmin):
    class Media:
        css = {
            'all': ('core/admin/app1/css/custom.css',) # Here
        }
        js = ('core/admin/app1/js/custom.js',) # Here

@admin.register(Food)
class FoodAdmin(admin.ModelAdmin):
    class Media:
        css = {
            'all': ('core/admin/app1/css/custom.css',) # Here
        }
        js = ('core/admin/app1/js/custom.js',) # Here

However, the above solution is not efficient, so I

<link ..."admin/css/base.css" %}{% endblock %}"> in base.html After setting them as shown below, but the solution below does not work:

# "templates/admin/app1/base.html"

# ...
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">
<link rel="stylesheet" href="{% static "core/admin/app1/css/custom.css" %}"> {# Here #}
<script src="{% static 'core/admin/app1/js/custom.js' %}" defer></script> {# Here #}
{% block dark-mode-vars %}
  <link rel="stylesheet" href="{% static "admin/css/dark_mode.css" %}">
# ...

So I put

base.html in templates/admin/ as shown below, then the above solution works but with custom css and js files will apply to all admins in all applications:

django-project
 |-core
 |  |-settings.py
 |  └-static
 |     └-core
 |        └-admin
 |           └-app1
 |              |-css
 |              |  └-custom.css
 |              └-js
 |                 └-custom.js
 |-app1
 |  |-models.py
 |  └-admin.py
 |-app2
 └-templates
    └-admin
       └-base.html # Here

So, how do I apply the custom

css and js files effectively to only all admins in app1, Person, Animal and Food?

P粉396248578
P粉396248578

reply all(1)
P粉545956597

I can't use comments.

1. Set static folder

STATICFILES_DIRS = [
    # add
    # os.path.join(BASE_DIR, 'core', 'static' ....)
]
....

2. Check router.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] 
# add
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

3. Check settings.py

# add
STATIC_URL = "static/"

4. Run command

python manage.py collectstatic

https://docs.djangoproject. com/ja/4.2/howto/static-files/#configuring-static-files

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template