jMy*_*les 25 django django-urls django-admin
尽管我喜欢django文档,但管理员中关于bookmarklet的部分却很奇怪.
我的问题是这样的:如果我在视图中并且我有一个django模型(或者,在某些情况下,是一个实际的对象),我怎样才能到达该模型(或对象)的相关管理页面?如果我有对象coconut_transportation.swallow.objects.all()[34],我怎么能直接跳到管理页面来编辑那个特定的燕子?
同样,如何获取管理页面的URL以添加另一个吞咽?
Yuj*_*ita 52
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls
obj = coconut_transportation.swallow.objects.all()[34]
# list url
url = reverse("admin:coconut_transportation_swallow_changelist")
# change url
url = reverse("admin:coconut_transportation_swallow_change", args=[obj.id])
# add url
url = reverse("admin:coconut_transportation_swallow_add")
Run Code Online (Sandbox Code Playgroud)
小智 12
你可以从实际的对象实例中检索这个,这对我有用:
from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(object.__class__)
object_admin_url = django.core.urlresolvers.reverse("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(object.pk,))
Run Code Online (Sandbox Code Playgroud)
见:http://djangosnippets.org/snippets/1916/
您实际上可以检索信息而无需查询ContentTypes
只需将其添加到您的模型类:
def get_admin_url(self):
return urlresolvers.reverse("admin:%s_%s_change" %
(self._meta.app_label, self._meta.model_name), args=(self.pk,))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21076 次 |
最近记录: |