Django Daisy

repository·main·Indexed 18 days ago

https://github.com/hypy13/django-daisy

A modern, responsive, and themeable UI skin for the Django Admin interface built with DaisyUI v5 and Tailwind CSS v4. It provides a customized dashboard, theme selection, and enhanced admin features such as tabbed inline admins, tabbed fieldsets, and customizable app appearance in the sidebar via AppConfig or DAISY_SETTINGS.

Tokens
4.3K
Snippets
17
Records
21
Agent score
63%

What's inside django-daisy

  1. Enable Language Switching in Admin

    main

    To support multiple languages and RTL (Right-to-Left) in the admin panel, follow these three steps:

    1. Add URL pattern in urls.py:

      path("i18n/", include("django.conf.urls.i18n"))
    2. Enable middleware in settings.py:

      MIDDLEWARE = [
          'django.middleware.locale.LocaleMiddleware',
          # ...
      ]
    3. Define languages in settings.py:

      LANGUAGES = [
          ('en', 'English'),
          ('fa', 'Farsi'),
      ]
    # urls.py
    urlpatterns = [
        path("i18n/", include("django.conf.urls.i18n")),
        # ...
    ]
  2. Configure Django Daisy in settings.py

    main

    To activate Django Daisy, add django_daisy to your INSTALLED_APPS. Note that django.contrib.humanize is a required dependency.

    INSTALLED_APPS = [
        'django_daisy',
        'django.contrib.admin',
        'django.contrib.humanize',  # Required
        # ... your other apps
    ]
  3. Install Django Daisy

    main

    You can install Django Daisy via PyPI for standard use or directly from GitHub for development purposes.

    Quick Install (PyPI):

    pip install django-daisy

    Development Install (GitHub):

    pip install -e git+https://github.com/hypy13/django-daisy.git#egg=django-daisy
  4. Configure app appearance and order in DaisyAdminSite

    main

    You can customize how apps appear in the DaisyAdminSite dashboard using two methods:

    1. Using DAISY_SETTINGS['APPS_REORDER']

    In your Django settings, define a dictionary where keys are app labels and values are configuration overrides. This is useful for quick adjustments without creating new AppConfig classes.

    2. Using AppConfig attributes

    DaisyAdminSite looks for specific attributes on your Django AppConfig objects. You can add these to your app's apps.py to define how the app is represented in the dashboard:

    • icon: The icon identifier (e.g., for Font Awesome).
    • divider_title: A title used to group the app under a specific section divider.
    • priority: An integer used to sort the app in the list (higher values appear first).
    • hide: A boolean to exclude the app from the admin index.
    # Example using DAISY_SETTINGS in settings.py
    DAISY_SETTINGS = {
        "APPS_REORDER": {
            "myapp": {
                "priority": 10,
                "icon": "fa-rocket",
            }
        }
    }
    
    # Example using AppConfig in myapp/apps.py
    class MyappConfig(AppConfig):
        name = 'myapp'
        icon = 'fa-rocket'
        divider_title = 'Core Modules'
        priority = 10
  5. Configure Global DAISY_SETTINGS

    main

    Use the DAISY_SETTINGS dictionary in settings.py to control branding, themes, and UI behavior.

    Branding

    • SITE_TITLE: The title of the site.
    • SITE_HEADER: The header text.
    • INDEX_TITLE: The welcome message on the dashboard.
    • SITE_LOGO: Path to the site logo.

    Customization

    • EXTRA_STYLES: List of additional CSS files.
    • EXTRA_SCRIPTS: List of additional JS files.
    • LOAD_FULL_STYLES: Set to True to load the complete DaisyUI library (required for custom themes).
    • SHOW_CHANGELIST_FILTER: Boolean to auto-open the filter sidebar.
    • DONT_SUPPORT_ME: Boolean to hide the GitHub link.
    • SIDEBAR_FOOTNOTE: Custom text for the sidebar footer.

    Theme Configuration

    • DEFAULT_THEME: The default light theme (e.g., 'corporate').
    • DEFAULT_THEME_DARK: The default dark theme (e.g., 'dim').
    • SHOW_THEME_SELECTOR: Boolean to show/hide the theme dropdown.
    • THEME_LIST: A list of dictionaries defining available themes: [{'name': 'Label', 'value': 'theme-value'}].

    Third-Party App Customization

    • APPS_REORDER: A dictionary to customize existing apps (like auth). Keys are app names, values are configuration objects containing icon, name, hide, and divider_title.
    DAISY_SETTINGS = {
        # Branding
        'SITE_TITLE': 'Django Admin',
        'SITE_HEADER': 'Administration',
        'INDEX_TITLE': 'Hi, welcome to your dashboard',
        'SITE_LOGO': '/static/admin/img/daisyui-logomark.svg',
        
        # Customization
        'EXTRA_STYLES': [],  # Additional CSS files
        'EXTRA_SCRIPTS': [],  # Additional JS files
        'LOAD_FULL_STYLES': False,  # Load complete DaisyUI library
        'SHOW_CHANGELIST_FILTER': False,  # Auto-open filter sidebar
        'DONT_SUPPORT_ME': False,  # Hide GitHub link
        'SIDEBAR_FOOTNOTE': '',  # Custom sidebar footer text
        
        # Theme Configuration
        'DEFAULT_THEME': None,  # e.g., 'corporate', 'dark'
        'DEFAULT_THEME_DARK': None,  # Dark mode default
        'SHOW_THEME_SELECTOR': True,  # Show/hide theme dropdown
        'THEME_LIST': [
            {'name': 'Light', 'value': 'light'},
            {'name': 'Dark', 'value': 'dark'},
            # Add custom themes...
        ],
        
        # Third-Party App Customization
        'APPS_REORDER': {
            'auth': {
                'icon': 'fa-solid fa-person-military-pointing',
                'name': 'Authentication',
                'hide': False,
                'divider_title': "Auth",
            },
        },
    }
  6. How django-daisy initializes the Admin Site

    main
    When the django_daisy app is loaded, it automatically replaces the default Django admin.site and django.contrib.admin.sites.site with an instance of the configured AdminSiteClass (defaulting to DaisyAdminSite). Additionally, it registers the Django LogEntry model with LogentryAdmin to provide enhanced logging visibility within the Daisy dashboard.
  7. Use Tabbed Fieldsets

    main

    You can convert standard Django admin fieldsets into navigation tabs by adding the 'navtab' class to the fieldset configuration.

    @admin.register(MyModel)
    class MyModelAdmin(admin.ModelAdmin):
        fieldsets = (
            (None, {
                'fields': ('username', 'password')
            }),
            ('Personal Info', {
                'fields': ('first_name', 'last_name', 'email'),
                'classes': ('navtab',),  # Creates a tab
            }),
            ('Permissions', {
                'fields': ('is_active', 'is_staff', 'is_superuser'),
            }),
        )
  8. Use Tabbed Inline Admin

    main

    To organize related objects into tabs within an admin change form, use the NavTabMixin from django_daisy.mixins on your Inline class.

    from django_daisy.mixins import NavTabMixin
    
    class ChoiceInline(admin.TabularInline, NavTabMixin):
        model = Choice
        extra = 1
    
    @admin.register(Poll)
    class PollAdmin(admin.ModelAdmin):
        inlines = [ChoiceInline]
  9. Customize App Appearance in the Sidebar

    main

    You can customize how your own apps appear in the admin sidebar by configuring the AppConfig class in your app's apps.py file.

    Supported attributes:

    • icon: A FontAwesome icon string (e.g., 'fa fa-square-poll-vertical').
    • divider_title: A string to create a section divider in the sidebar.
    • priority: An integer for sidebar ordering (higher values appear at the top).
    • hide: A boolean to hide the app from the sidebar.
    class PollsConfig(AppConfig):
        name = 'polls'
        icon = 'fa fa-square-poll-vertical'  # FontAwesome icon
        divider_title = "Apps"  # Section divider title
        priority = 0  # Sidebar ordering (higher = top)
        hide = False  # Hide from sidebar
  10. Configure the default Admin Site class

    main

    By default, django-daisy uses django_daisy.admin.DaisyAdminSite as the primary admin site. You can override this behavior by setting the DEFAULT_ADMIN_SITE_CLASS option in your Django settings.py. This value should be a string representing the full import path to your custom admin site class.

    # settings.py
    DEFAULT_ADMIN_SITE_CLASS = 'my_project.admin.MyCustomAdminSite'