unfoldadmin / django-unfold

Modern Django admin theme for seamless interface development
https://unfoldadmin.com
MIT License
1.82k stars 184 forks source link

add additional page - such as analytics in demo #208

Closed TristanBrotherton closed 11 months ago

TristanBrotherton commented 11 months ago

In the demo there is a navigation component that shows "analytics" as an example. Is there any documentation on how to add an additional page in the admin interface such as this analytics page - and be linked by the navigation component and render the rest of the unfold UI as the index.html page does?

If i create a new view, and render a new page

# analytics/views.py
from django.contrib.admin.views.decorators import staff_member_required
from django.shortcuts import render

@staff_member_required
def analytics_view(request):
    context = {
    }
    return render(request, 'admin/analytics.html', context)

and that page contains:

{% extends 'unfold/layouts/base_simple.html' %}
{% load i18n unfold %}
{% block breadcrumbs %}{% endblock %}
{% block title %}
    {% trans 'Dashboard' %} | {{ site_title|default:_('Django site admin') }}
{% endblock %}
{% block branding %}
    <h1 id="site-name">
        <a href="{% url 'admin:index' %}">
            {{ site_header }}
        </a>
    </h1>
{% endblock %}

{% block content %}
    {% component "unfold/components/container.html" %}
        {% component "unfold/components/flex.html" with class="gap-4" %}
            {% component "unfold/components/navigation.html" with items=navigation %}{% endcomponent %}
            {% component "unfold/components/navigation.html" with class="ml-auto" items=filters %}{% endcomponent %}
        {% endcomponent %}
        {% component "unfold/components/flex.html" with class="gap-8 mb-8 flex-col lg:flex-row" %}
            my stuff
        {% endcomponent %}
    {% endcomponent %}
{% endblock %}

The rendered page doesn't contain any of the unfold UI I presume as it wasn't provided required context. Additionally I'm also unsure how to provide the link of this page to the navigation component.

Cacsjep commented 11 months ago

did u found a solution ?

erwaen commented 3 months ago

@Cacsjep did you found a solution?

Cacsjep commented 3 months ago

I dont use Django anymore because some very dynamic out of Standart stuff things are always a mess if Django not your day to day Business

bgervan commented 2 months ago

In case others looking for a solution without registering a model:

# views.py

class SampleClassBasedView(UnfoldAdminViewMixin, TemplateView):
    title = "Custom Title"  # required: custom page header title
    permissions_required = ()  # required: tuple of permissions
    template_name = "admin/sample.html"

    def has_permission(self):
        return self.request.user.is_superuser

where the UnfoldAdminViewMixin

# mixins.py

from typing import Any, Dict

from django.contrib import admin
from django.contrib.auth.mixins import PermissionRequiredMixin
from unfold.exceptions import UnfoldException

class UnfoldAdminViewMixin(PermissionRequiredMixin):
    """
    Prepares views to be displayed in admin
    """
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
        if not hasattr(self, "title"):
            raise UnfoldException(
                "UnfoldModelAdminViewMixin was not provided with 'title' attribute"
            )

        return super().get_context_data(
            **kwargs,
            **admin.site.each_context(self.request),
            **{"title": self.title},
        )

@lukasvinclav If you are open to it, I can create a PR and add some doc. I saw more issues related to this.