Monday 26 July 2021

Django admin, page not found in custom view

I encountered very annoying problem.

I have created my own AdminSite like this:

from django.contrib import admin
from django.template.response import TemplateResponse
from django.urls import path

class MyAdminSite(admin.AdminSite):

    def get_urls(self):
        urls = super().get_urls()
        my_urls = [
            path('statistics/', self.admin_view(self.statistics), name='statistics'),
        ]
        return urls + my_urls

    def statistics(self, request):
        context = dict(
            self.each_context(request),
        )
        return TemplateResponse(request, 'admin/statistics.html', context)

I have created my own AdminConfig and assigned it into my INSTALLED_APPS, created html file, then in my root urls added it like this:

 urlpatterns = [
    url(r'^admin/', admin.site.urls),
 ]

I have logged in into my admin page and when I'm trying to open localhost:8000/admin/statistics I receive this:

Page not found (404)
Request URL:    http://localhost:8000/admin/statistics/

Why this is happening? Did I miss something?

Update

I have added print on my get_urls and it showed this.(I removed unnecessary urls):

[
<URLPattern '' [name='index']>, 
<URLPattern 'login/' [name='login']>, 
<URLPattern 'logout/' [name='logout']>, 
<URLPattern 'password_change/' [name='password_change']>, 
<URLPattern 'password_change/done/' [name='password_change_done']>, 
<URLPattern 'autocomplete/' [name='autocomplete']>, 
<URLPattern 'jsi18n/' [name='jsi18n']>, 
<URLPattern 'r/<int:content_type_id>/<path:object_id>/' [name='view_on_site']>, 
<URLResolver <URLPattern list> (None:None) 'auth/group/'>,  
<URLPattern '(?P<url>.*)$'>, 
<URLPattern 'statistics/' [name='statistics']>
]

Using python manage.py show_urls | grep statistics shows me this:

/admin/statistics/   project.admin.statistics    admin:statistics


from Django admin, page not found in custom view

No comments:

Post a Comment