Django 4.2.6: Installing Different Admin Templates for Different Custom Admin Sites

97 Views Asked by At

I'm facing an issue while trying to install different admin templates for different custom admin sites in Django. Specifically, I want to use the default django.contrib.admin template for the default admin site and enable django-jet-reboot for a custom admin site.

The problem I'm encountering is that when I install django-jet-reboot, it gets enabled for all admin sites, including the default one.

Could someone please guide me on how to correctly install and apply different admin templates for distinct custom admin sites in Django?

Thank you in advance for your assistance!

I have tried to write a custom djanto template loader that reads a field from the settings: The following is a configuration that assiciate a given admin site with a corresponding template

#settings.py
.............
ADMIN_TEMPLATE_ASSOCIATION = {
    'admin': {
        "template": "django.*",
        "exclude_templates": [
            'jet.*',
            'jet.forms',
            'jet.dashboard',
        ]
    },
    'first_admin': {
        "template": "jet.*",
        "exclude_templates": [
        ]
    },
}
..............

""" Wrapper for loading templates from "templates" directories in INSTALLED_APPS packages. """

#myapp.loader.py

from pathlib import Path
import re
from django.template.utils import get_app_template_dirs

from .filesystem import Loader as FilesystemLoader
"""
Wrapper for loading templates from "templates" directories in INSTALLED_APPS
packages.
"""

from django.template.utils import get_app_template_dirs

from .filesystem import Loader as FilesystemLoader
from django.conf import settings

from django.apps import apps

def get_app_template_dirs(suffix='templates', admin_site_name="first_admin"):
    """
    Return an iterable of paths of directories to load app templates from.

    admin_site_name is the name of the admin site.
    """
    template_dirs = []

    app_name = "*"
    exclude_apps = []
    # Check if the admin site is associated with an app in settings
    if hasattr(settings, 'ADMIN_TEMPLATE_ASSOCIATION'):
        association = settings.ADMIN_TEMPLATE_ASSOCIATION.get(admin_site_name)
        if association:
            # Get template directories for the associated app
            app_name = association.get("template")
            exclude_apps = association.get("exclude_templates")
            template_dirs = get_app_template_dirs_by_app(association)

    # Get template directories for other apps
    for app_config in apps.get_app_configs():
        if not re.match(app_name, app_config.name) and \
            not any(re.match(pattern, app_config.name) for pattern in exclude_apps):
            template_dirs.extend([Path(app_config.path) / 'templates'])

    return tuple(template_dirs)

def get_app_template_dirs_by_app(association):
    """
    Return template directories for a specific app.
    """
    template_dirs = []
    exclude_apps = association.get("exclude_templates")
    app_name = association.get("template")
    
    for app_config in apps.get_app_configs():
        if re.match(app_name, app_config.name) and \
            not any(re.match(pattern, app_config.name) for pattern in exclude_apps):
            template_dirs = [Path(app_config.path) / 'templates'] + template_dirs
        template_dirs.extend([Path(app_config.path) / 'forms' / 'templates'])
    return template_dirs


class CustomLoader(FilesystemLoader):
    def get_dirs(self):
        print(get_app_template_dirs())
        return get_app_template_dirs()


class Loader(FilesystemLoader):
    def get_dirs(self):
        return get_app_template_dirs("templates")

There are two problems:

  1. The main probleme I dont know how to get the active admin site from the loader file.
  2. when I pass the admin site manualy it works but the page displays with no formatting
0

There are 0 best solutions below