如何从视图生成到Django管理站点中的特定项的url ?

时间:2022-12-26 07:22:06

I would like to make a link that would take the user to a particular item in the admin site (assuming they have the correct permissions).

我想要创建一个链接,将用户带到管理站点中的某个特定项目(假设他们有正确的权限)。

Something like: https://mysite/admin/app/model/id/

比如:https://mysite/admin/app/model/id/

Can this be done with reverse?

这能反过来做吗?

1 个解决方案

#1


16  

You can get the url in the view, using reverse,

你可以在视图中获取url,使用反向,

object_change_url = reverse('admin:myapp_mymodel_change', args=(obj.id,))

Or in the template, using the url tag

或者在模板中,使用url标记

{% url 'admin:myapp_mymodel_change' obj.id %}

or

{% load admin_urls %}
{% url obj|admin_urlname:'change' obj.id %}">

Note the above url tag syntax is for Django >= 1.5.

注意,上面的url标记语法为Django >= 1.5。

For more information, see the Django docs on reversing admin urls.

有关更多信息,请参阅Django文档,了解反向管理url。

#1


16  

You can get the url in the view, using reverse,

你可以在视图中获取url,使用反向,

object_change_url = reverse('admin:myapp_mymodel_change', args=(obj.id,))

Or in the template, using the url tag

或者在模板中,使用url标记

{% url 'admin:myapp_mymodel_change' obj.id %}

or

{% load admin_urls %}
{% url obj|admin_urlname:'change' obj.id %}">

Note the above url tag syntax is for Django >= 1.5.

注意,上面的url标记语法为Django >= 1.5。

For more information, see the Django docs on reversing admin urls.

有关更多信息,请参阅Django文档,了解反向管理url。