简体   繁体   中英

Custom templates for custom AdminSite Django

I have two admin sites and I want to have different custom templates on both of them This is my admin.py file:

from django.contrib import admin
from django.contrib.admin import AdminSite
from .models import Equipo

@admin.register(Equipo)
class EquipoAdmin(admin.ModelAdmin):
    list_display = ('codigo', 'nombre', 'contador', 'unidades')

class AdminMantenimiento(AdminSite):
    site_header = "MANTENIMIENTO"

class EquipoAdminMantenimiento(admin.ModelAdmin):
    list_display = ('codigo', 'nombre')

admin_site = AdminMantenimiento(name='Administrador Mantenimiento')
admin_site.register(Equipo, EquipoAdminMantenimiento)

This is my urls.py file:

from django.contrib import admin
from django.urls import path
from Mantenimiento.admin import admin_site

urlpatterns = [
    path('admin/', admin.site.urls),
    path('admin2/',admin_site.urls)
]

If I override the templates as per Django documentation changes would be applied to both AdminSites. How can I set custom templates for the class extending AdminSite?

The only way I could think to do this would be to explicitly override the url of the admin page you want to customize and point it to a different template. For example to customize a template in admin2/ but not admin1/

from django.contrib import admin
from django.urls import path
from Mantenimiento.admin import admin_site
from app.views import *

urlpatterns = [
    path('admin/', admin.site.urls),
    path('admin2/app/model', TemplateView.as_view(template_name='model_admin.html')),
    path('admin2/', admin_site.urls)
]

This will only work if the more specific url definition ( admin2/app/model ) comes before the less specific definition ( admin2/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM