简体   繁体   English

从1.8升级到1.9 Django Admin get_urls无法正常工作

[英]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. 我正在使用Django管理员,刚刚从1.8升级到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. 在1.8中,我在change_form中添加了一个单击按钮,它使用get_urls覆盖将我带到另一个html模板。 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. 具有主键'132 / change / tarrespgraph'的NBI Graph对象不存在。

Django finds the customized change_form.html without a problem. Django发现自定义的change_form.html没有问题。 My custom template (tarrespgraph.html) is in the same folder as my customized change_form.html. 我的自定义模板(tarrespgraph.html)与我自定义的change_form.html位于同一文件夹中。 Where is Django looking for my custom template? Django在哪里寻找我的自定义模板? Should I move the tarrespgraph.html, or change the reference to the url? 我应该移动tarrespgraph.html,还是更改对url的引用? Thanks in advance for your assistance! 提前感谢你的帮助!

You probably shouldn't have removed the (\\d+) group from your url pattern. 您可能不应该从您的网址格式中删除(\\d+)组。 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. 如果没有(\\d+)组,新的url模式与url不匹配,因此它由admin更改视图处理,该视图会给出错误。

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: 在Django 1.9中,Django已将change附加到管理员更改网址(例如,现在是/admin/app/model/132/change/而不是/admin/app/model/132/ 。这意味着您的相对链接'tarrespgraph/'现在指向/admin/app/model/132/change/tarrespgraph/而不是/admin/app/model/132/tarrespgraph/ 。您可以将相对链接更改为../tarrespgraph/ 。但是,它会更少脆弱使用url标签代替:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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