Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin, extending admin with custom views

Tags:

python

django

I would like to request some assistance regarding this matter.

I have followed this guide to add a view to my admin.

I am using the same code that the site has and I'm getting an error:

Reverse for 'app_list' with arguments '()' and keyword arguments '{'app_label': 'reports'}' not found.

The admin/my_view works fine but if I try to access the other pages in the admin such as the index page I am getting the error

Here's the code:

#someapp/admin.py

from django.conf.urls import patterns
from django.contrib import admin
from django.http import HttpResponse

def my_view(request):
    return HttpResponse("Hello!")

def get_admin_urls(urls):
    def get_urls():
        my_urls = patterns('',
            (r'^my_view/$', admin.site.admin_view(my_view))
         )
        return my_urls + urls
    return get_urls

admin_urls = get_admin_urls(admin.site.get_urls())
admin.site.get_urls = admin_urls

I am using django 1.8.2, python 2.7

also, what is the best way to add some view or context in the admin's index page?

UPDATE

I have tried Antoine Pinsard's answer and tried this:

#admin.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):

    def get_urls(self):
        from django.conf.urls import url
        urls = super(MyAdminSite, self).get_urls()
        urls += [
            url(r'^my_view/$', self.admin_view(self.my_view))
        ]
        return urls

    def my_view(self, request):
        return HttpResponse("Hello!")

admin_site = MyAdminSite()

And replacing urls.py's url(r'^admin/', include(admin.site.urls)), to url(r'^admin/', include(admin_site.urls)),

and getting:

Reverse for 'app_list' with arguments '()' and keyword arguments

TRACEBACK:

Request Method: GET
Request URL: http://localhost:8000/@dmin/

Django Version: 1.8.2
Python Version: 2.7.10
Installed Applications:
('grappelli',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sessions.models',
 'frontend',
 'file_maintenance',
 'reports',
 'transactions',
 'admin_reorder',
 'admin_notifications',
 'django_twilio',
 'daterange_filter')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.models.User',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'admin_reorder.middleware.ModelAdminReorder')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  157.                     response = middleware_method(request, response)
File "C:\Python27\lib\site-packages\admin_reorder\middleware.py" in process_template_response
  134.         self.init_config(request, app_list)
File "C:\Python27\lib\site-packages\admin_reorder\middleware.py" in init_config
  21.         admin_index = admin.site.index(request)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in index
  438.                                 current_app=self.name,
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in reverse
  579.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
  496.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /@dmin/
Exception Value: Reverse for 'app_list' with arguments '()' and keyword arguments '{'app_label': 'django_twilio'}' not found. 0 pattern(s) tried: []
like image 828
Juan Carlos Asuncion Avatar asked Mar 08 '16 18:03

Juan Carlos Asuncion


2 Answers

This guide looks quite old. I would rather advise you to follow django docs.

someapp/admin.py

from django.contrib.admin import AdminSite
from django.http import HttpResponse

class MyAdminSite(AdminSite):

     def get_urls(self):
         from django.urls import path
         urls = super().get_urls()
         urls += [
             path('my_view/', self.admin_view(self.my_view))
         ]
         return urls

     def my_view(self, request):
         return HttpResponse("Hello!")

admin_site = MyAdminSite()

Source: https://github.com/django/django/blob/2.2/django/contrib/admin/sites.py#L194-L205

You should also update your project/urls.py and replace path('admin/', admin.site.urls) by path('admin/', admin_site.urls). Don't forget to from someapp.admin import admin_site before.

like image 171
Antoine Pinsard Avatar answered Sep 20 '22 07:09

Antoine Pinsard


The guide you linked is old and I was surprised to not find anything directly answering your question in the last year or so.

  1. Create a new Admin Site in your app's admin.py or in a convenient place.
  2. Create a function in the new AdminSite that augments the get_urls() function with your extra urls.
  3. Make sure your project urls.py links to the newly created AdminSite.

The below works with Python 3.5.1 and Django 1.9.6.


my_app/admin.py

from django.contrib import admin
from django.contrib.admin import AdminSite
from django.http import HttpResponse

from my_app.models import SomeModel


class MyAdminSite(AdminSite):

    def custom_view(self, request):
        return HttpResponse("Test")

    def get_urls(self):
        from django.conf.urls import url
        urls = super(MyAdminSite, self).get_urls()
        urls += [
            url(r'^custom_view/$', self.admin_view(self.custom_view))
        ]
        return urls

admin_site = MyAdminSite()


@admin.register(SomeModel, site=admin_site)
class SomeModelAdmin(admin.ModelAdmin):
    pass

my_project/urls.py

from django.conf.urls import url, include

from my_app.admin import admin_site

urlpatterns = [
    url(r'^admin/', admin_site.urls),
    ...
]
like image 37
Daniel van Flymen Avatar answered Sep 23 '22 07:09

Daniel van Flymen