Django: Link to own AdminSite in template of TemplateView

I defined my own AdminSite in Django 5.0.2 and added some APPs to this AdminSite. This is working and I can access my own admin page via http://127.0.0.1:8080/my-admin/. Now I would like to add the link of this admin-page ("my-admin") to a template of a templateView. (see the ????????? in the main.html file). The link to the standard admin-page is working fine.

I am a beginner with Django and tried a few days to get this up&running, but unfortunately I could not find any solution. Could someone give me an hint, please?

mydjango/myadmin/admin.py:

from django.contrib.admin import AdminSite
    
class EventAdminSite(AdminSite):
        site_header = 'Test ADMIN'
        site_titile = 'Test ADMIN PORTAL'
        index_title = 'WELCOME TO MY ADMIN PORTAL'
    
my_admin_site = EventAdminSite(name = 'my_admin')

mydjango/applications/admin.py:

from django.contrib import admin
from .models import Application
from myadmin.admin import my_admin_site

class ApplicationAdmin(admin.ModelAdmin):
    search_fields = ["name", "dest1_protocol","dest1_port"]
    exclude = ["created","modified"]   
    
   
admin.site.register(Application)
my_admin_site.register(Application,ApplicationAdmin)

mydjango\mysettings.py:

urlpatterns = [
       ..
       path("my-admin/",my_admin_site.urls),
       ..
]

mydjango/main/views.py:

from django.shortcuts import render

from django.views.generic.base import TemplateView

class IndexView(TemplateView):
    template_name = 'main/index.html'
    
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['title'] = "Policy-Management"
        return context

mydjango/main/templates/main.html

{% load admin_urls %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ title }}</title>
</head>
<body>
    {{ title }}
    <a href="{% url 'admin:index' %}">link to admin panel</a>
    <a href="{% url '???????????????????????' %}">link to my admin panel</a>

</body>
</html>

It should be {% url 'my_admin:index' %}.

When constructing an instance of an AdminSite, you can provide a unique instance name using the name argument to the constructor. This instance name is used to identify the instance, especially when reversing admin URLs.

https://docs.djangoproject.com/en/5.0/ref/contrib/admin/#django.contrib.admin.AdminSite

These named URLs are registered with the application namespace admin, and with an instance namespace corresponding to the name of the Site instance.

https://docs.djangoproject.com/en/5.0/ref/contrib/admin/#admin-reverse-urls

More about URL namespaces: https://docs.djangoproject.com/en/5.0/topics/http/urls/#url-namespaces

Back to Top