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
<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 # HereSo, how do I apply the custom
css and
js files effectively to only all admins in
app1,
Person,
Animal and
Food?
P粉5459565972024-04-04 12:10:56
I can't use comments.
STATICFILES_DIRS = [ # add # os.path.join(BASE_DIR, 'core', 'static' ....) ] ....
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)
# add STATIC_URL = "static/"
python manage.py collectstatic
https://docs.djangoproject. com/ja/4.2/howto/static-files/#configuring-static-files