简体   繁体   中英

Upgrading from 1.8 to 1.9 Django Admin get_urls not working

I am using the Django admin and have just upgraded from 1.8 to 1.9. In 1.8, I added a click button to the change_form that takes me to another html template using the get_urls override. Like this:

def get_urls(self):
    urls = super(arunAdmin, self).get_urls()
    my_urls = patterns('',
        (r'(\d+)/tarrespgraph/$', self.admin_site.admin_view(self.tarrespgraph)),
        )
return my_urls + urls

Following some of the recommendations I saw online, I have changed this to:

def get_urls(self):
    urls = super(arunAdmin, self).get_urls()
    my_urls = [
        url(r'^tarrespgraph/$', self.admin_site.admin_view(self.tarrespgraph)),
    ]        
    return my_urls + urls

But am receiving this error:

NBI Graph object with primary key '132/change/tarrespgraph' does not exist.

Django finds the customized change_form.html without a problem. My custom template (tarrespgraph.html) is in the same folder as my customized change_form.html. Where is Django looking for my custom template? Should I move the tarrespgraph.html, or change the reference to the url? Thanks in advance for your assistance!

You probably shouldn't have removed the (\\d+) group from your url pattern. Try the following:

my_urls = [
    url(r'^(\d+)/tarrespgraph/$', self.admin_site.admin_view(self.tarrespgraph), name='tarrespgraph'),
]

Note that I've added a name, which will let us reverse the url later.

Without the (\\d+) group, the new url pattern does not match the url, so it is handled by the admin change view which gives the error.

You also need to change the link in your template. In Django 1.9, Django has appended change to the admin change url (eg it is now /admin/app/model/132/change/ instead of /admin/app/model/132/ . That means that your relative link 'tarrespgraph/' now points to /admin/app/model/132/change/tarrespgraph/ instead of /admin/app/model/132/tarrespgraph/ . You could change the relative link to ../tarrespgraph/ . However, it would be less fragile to use the url tag instead:

<a class="tarrespgraph" href="{% url 'admin:tarrespgraph' object_id %}">

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