hdrz.cc

Adding views to admin site in django

An old post resurected from blogger. I leared python by making sites with django. Maybe this is still relevant, I have no idea.


I’m using django 1.1 now, and custom views on the admin site that worked before are now broken. It turns out that you need to add the ‘admin’ namespace to the url tag in templates. For example:

class SubscriptionAdmin(admin.ModelAdmin):
    # define custom views for the admin
    def export_csv_view(self, request):
        return export_csv(request, self.model)

    # hook custom views into admin site urls
    def get_urls(self):
        urls = super(SubscriptionAdmin, self).get_urls()
        csv_urls = patterns('',
            url(r'^export-csv/$',
                self.export_csv_view,
                name='export_csv'),
        )
        return csv_urls + urls
    ...
    ...

Using this view in a template, you could write

href="{% url export_csv %}"

but now it works like this:

href="{% url admin:export_csv %}"

–[may the namespace be with you]–

#django  #admin-site  #get_urls