简体   繁体   中英

How to reverse a custom admin url in Django 1.5?

I want to add a custom url for a ModelAdmin but it seems Django changed template syntax in 1.5. I tried this way as the documentation pointed out but unfortunately it doesn't work.

class VariableAdmin(admin.ModelAdmin):

    def get_urls(self):
         urls = super(VariableAdmin, self).get_urls()
         my_urls = patterns('',
                       url(r'^settings/([A-Za-z0-9]*)', self.admin_site.admin_view(self.settings), name="settings"))
         return my_urls + urls

    def settings(self, request, category):
         return render_to_response('variables.html', {"opts": Variable._meta}))

Here is my variables.html

{% load admin_urls %}
{% url opts|admin_urlname:'settings' %}

This code throws an error like this:

Reverse for 'common_variable_settings' with arguments '()' and keyword arguments '{}' not found.

How can I fix this problem?

Try changing name of the view to include application and model names:

...
my_urls = patterns('', url(r'^settings/([A-Za-z0-9]*)',
                   self.admin_site.admin_view(self.settings), 
                   name="common_variable_settings"))
...

The admin_urlname template filter returns "full" names, see it's source code:

@register.filter
def admin_urlname(value, arg):
    return 'admin:%s_%s_%s' % (value.app_label, value.module_name, arg)

So definitely You need to name Your view "appname_modulename_settings" . Maybe then try changing regular expression to something like this:

r'^settings/([A-Za-z0-9]+/)?$'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM