Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override AdminSite to append custom urls

I overrode default AdminSite class as described in manual, though it's too pure with information about this part over there.
My gs/admin.py file:

from django.contrib.admin import AdminSite
from django.conf.urls import patterns, url
from gs.views import *

class AdminSiteGs(AdminSite):

    def get_urls(self):
        urls = super(AdminSiteGs, self).get_urls()

        urls += patterns('',
             url(r'^my_admin_view/$', self.admin_view(my_admin_view))
        )

        return urls

admin_site_gs = AdminSiteGs()

gs it's my application and project name.

gs/urls.py file:

from django.conf.urls import patterns, include, url
from page import views
from gs.admin import admin_site_gs

urlpatterns = patterns('',
    url(r'^admin/', include(admin_site_gs.urls)),
)

and I have application named page, where I place admin.py file:

from gs.admin import admin_site_gs
from page.models import Page, Menu
from django.contrib import admin

class PageAdmin(admin.ModelAdmin):
    list_display = ('name', 'url', 'page_type')

class MenuAdmin(admin.ModelAdmin):
    list_display = ('name', 'code')

admin_site_gs.register(Page, PageAdmin)
admin_site_gs.register(Menu, MenuAdmin)

So nothing here is working =( Neither /admin/my_admin view ( it returns 404 ), nor main admin page /admin. I don't see my models I registered in page/admin.py file.

It may sounds fun, but I tried all staff working in 3-4 hours =)) As you might guess I'm completely newbie both in Django and Python... All I want to know now is how to append custom URLs and views to my overriden class of AdminSite?

I removed autodiscover method, so now seems Django doesn't see nothing about file page/admin.py.

But the first question is more interesting, why I've had 404 error on trying to access the /admin/my_admin page ...

PS why my greeting at the beginning has been cutted o_O

like image 209
James May Avatar asked Feb 17 '23 06:02

James May


2 Answers

According to the docs, any URL patterns you define for custom admin views must be occur before the admin patterns: https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls

Try:

def get_urls(self):
    urls = super(AdminSiteGs, self).get_urls()

    my_urls = patterns('',
        url(r'^my_admin_view/$', self.admin_view(my_admin_view))
    )

    return my_urls + urls

You shouldn't need to include these patterns like this:

urlpatterns = patterns('',
    url(r'^admin/', include(admin_site_gs.urls)),  # not needed
)
like image 188
Brandon Avatar answered Feb 19 '23 20:02

Brandon


In my case, I had to override the default 'add url' in order to redirect to a custom Django admin page when clicking '+Add' button in the admin. So if I just override get_urls() in the way @Brandon said, it will return a list with a duplicate 'add' url (the custom one and the one retrieved from the super).

    def get_urls(self):
        info = self.model._meta.app_label, self.model._meta.model_name
        urls = super(RetailerAdmin, self).get_urls()
        # We need to remove the original 'add_url' in order to use the custom one.
        urls.remove(urls[1])
        custom_url = [
            url(r'^batch/$', self.admin_site.admin_view(self.batch_upload_retailers),
                name='%s_%s_add' % info),
        ]
        return custom_url + urls

To solve this, I removed the original 'add' url (notice that the 'add' url is always in position 1).

like image 38
mathias.lantean Avatar answered Feb 19 '23 20:02

mathias.lantean