Heim  >  Fragen und Antworten  >  Hauptteil

Wie kann man benutzerdefinierte „CSS“- und „JS“-Dateien effektiv auf alle Administratoren in einer Anwendung anwenden?

Ich habe einen Brauch cssjs 文件、admin.py 和覆盖的 base.html 位于 templates/admin/app1/ wie diesen:

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

Zuerst werde ich cssjs 文件分别设置为 c 中所有管理员 PersonAnimalFood 的 Media 类中的 cssendcphpcn phpcnapp1 如下所示,然后我可以将它们应用到所有管理员 PersonAnimalFood app1 anpassen:

# "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

Die obige Lösung ist jedoch nicht effizient, daher habe ich sie wie unten gezeigt nach <link ..."admin/css/base.css" %}{% endblock %}"> in base.html eingestellt, aber die folgende Lösung funktioniert nicht:

# "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" %}">
# ...

Also habe ich die base.html 放入 templates/admin/ 中,如下所示,然后上述解决方案有效,但自定义 cssjs -Datei, die für alle Administratoren gilt, in alle Anwendungen eingefügt:

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

Also, wie kann man cssjs 文件仅有效地应用于 app1 中的所有管理员 PersonAnimalFood anpassen?

P粉396248578P粉396248578179 Tage vor416

Antworte allen(1)Ich werde antworten

  • P粉545956597

    P粉5459565972024-04-04 12:10:56

    我无法使用评论。

    1。设置静态文件夹

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

    2。检查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。检查设置.py

    # add
    STATIC_URL = "static/"
    

    4。运行命令

    python manage.py collectstatic
    

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

    Antwort
    0
  • StornierenAntwort