获取对象的 Django 管理 url [英] Getting Django admin url for an object

查看:24
本文介绍了获取对象的 Django 管理 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Django 1.0 之前,有一种简单的方法可以获取对象的 admin url,我编写了一个小过滤器,我会像这样使用它:<a href="{{ object|admin_url }}" .... >... </a>

Before Django 1.0 there was an easy way to get the admin url of an object, and I had written a small filter that I'd use like this: <a href="{{ object|admin_url }}" .... > ... </a>

基本上我使用的是 url reverse 函数,视图名称为 'django.contrib.admin.views.main.change_stage'

Basically I was using the url reverse function with the view name being 'django.contrib.admin.views.main.change_stage'

reverse( 'django.contrib.admin.views.main.change_stage', args=[app_label, model_name, object_id] )

获取网址.

正如您可能已经猜到的那样,我正在尝试更新到最新版本的 Django,这是我遇到的障碍之一,获取管理员 url 的方法不再起作用.

As you might have guessed, I'm trying to update to the latest version of Django, and this is one of the obstacles I came across, that method for getting the admin url doesn't work anymore.

我如何在 django 1.0 中做到这一点?(或者 1.1,因为我正在尝试更新到 svn 中的最新版本).

How can I do this in django 1.0? (or 1.1 for that matter, as I'm trying to update to the latest version in the svn).

推荐答案

我有一个类似的问题,我会尝试调用 reverse('admin_index') 并且不断得到 django.core.urlresolvers.NoReverseMatch 错误.

I had a similar issue where I would try to call reverse('admin_index') and was constantly getting django.core.urlresolvers.NoReverseMatch errors.

原来我的 urls.py 文件中有旧格式的管理 url.

Turns out I had the old format admin urls in my urls.py file.

我的 urlpatterns 中有这个:

I had this in my urlpatterns:

(r'^admin/(.*)', admin.site.root),

这可以让管理屏幕正常工作,但已被弃用.我需要将其更改为:

which gets the admin screens working but is the deprecated way of doing it. I needed to change it to this:

(r'^admin/', include(admin.site.urls) ),

一旦我这样做了,Reversing Admin URLs 文档开始工作了.

Once I did that, all the goodness that was promised in the Reversing Admin URLs docs started working.

这篇关于获取对象的 Django 管理 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆