我怎样才能将用户重定向到不同的应用程序上保存?重定向管理员保存
我有两个应用程序,说app1
和app2
。如果用户在app2
点击保存,那么它应该被重定向到app1
而不是默认页面。
我不想做一个自定义表单。
我怎样才能将用户重定向到不同的应用程序上保存?重定向管理员保存
我有两个应用程序,说app1
和app2
。如果用户在app2
点击保存,那么它应该被重定向到app1
而不是默认页面。
我不想做一个自定义表单。
高清change_view(个体经营,要求,OBJECT_ID,extra_context中用=无):
result = super(mymodeladmin, self).change_view(request, object_id, extra_context)
result['Location'] = "your location"
return result
这不是一个好的答案:如果change_view不成功会发生什么?另外赋值给结果['Location']不是非常类似Django的(即使它可能工作)。上面的答案(Daniel Roseman)是一个很好的答案。 – 2011-11-08 18:02:40
你应该验证Roseman的答案以获得2点声望:)你可以学习如何使用stackexchange [here](http://meta.stackoverflow.com/) – 2011-12-08 11:17:32
@Glide - 什么是验证(它与upvoting,我做过的)。您的文档链接有点宽泛。 – 2011-12-28 15:05:59
要在保存到管理员后更改重定向目标,您需要覆盖ModelAdmin
类中的response_add()
(用于添加新实例)和response_change()
(用于更改现有的)。
请参阅原始码django.contrib.admin.options
。
如果你希望人们在StackOverflow上不断帮助你,你需要接受你的问题的答案。
简单的例子,以使其更清晰如何做到这一点(将是一个类的ModelAdmin内):
from django.core.urlresolvers import reverse
def response_add(self, request, obj, post_url_continue=None):
"""This makes the response after adding go to another apps changelist for some model"""
return HttpResponseRedirect(reverse("admin:otherappname_modelname_changelist"))
def response_add(self, request, obj, post_url_continue=None):
"""This makes the response go to the newly created model's change page
without using reverse"""
return HttpResponseRedirect("../%s" % obj.id])
也许看看URL命名空间......你也许可以使用HttpResponseRedirect +反向重定向您的重写保存方法内的用户。请记住,这是Django 1.1中的一项新功能,并且在1.0.x中不受支持。
http://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces
要添加到@ DanielRoseman的答案,你不希望用户重定向当他们选择保存并继续而不是保存按钮,那么您可以改用此解决方案。
def response_add(self, request, obj, post_url_continue="../%s/"):
if '_continue' not in request.POST:
return HttpResponseRedirect(get_other_app_url())
else:
return super(MyModelAdmin, self).response_add(request, obj, post_url_continue)
def response_change(self, request, obj):
if '_continue' not in request.POST:
return HttpResponseRedirect(get_other_app_url())
else:
return super(MyAdmin, self).response_change(request, obj)
谢谢,这真的帮了我。 – nym 2015-10-19 20:52:57
为什么没有接受任何问题的答案? – 2009-08-27 09:41:29
保存在哪里?在管理员中,以自定义的形式,在哪里? – 2009-08-27 09:50:53
在没有定制form.it管理是简单管理,我重写保存功能 – ha22109 2009-08-27 10:01:28