2009-08-27 76 views
22

我怎样才能将用户重定向到不同的应用程序上保存?重定向管理员保存

我有两个应用程序,说app1app2。如果用户在app2点击保存,那么它应该被重定向到app1而不是默认页面。

我不想做一个自定义表单。

+4

为什么没有接受任何问题的答案? – 2009-08-27 09:41:29

+0

保存在哪里?在管理员中,以自定义的形式,在哪里? – 2009-08-27 09:50:53

+0

在没有定制form.it管理是简单管理,我重写保存功能 – ha22109 2009-08-27 10:01:28

回答

-7

高清change_view(个体经营,要求,OBJECT_ID,extra_context中用=无):

result = super(mymodeladmin, self).change_view(request, object_id, extra_context) 

result['Location'] = "your location" 

return result 
+4

这不是一个好的答案:如果change_view不成功会发生什么?另外赋值给结果['Location']不是非常类似Django的(即使它可能工作)。上面的答案(Daniel Roseman)是一个很好的答案。 – 2011-11-08 18:02:40

+0

你应该验证Roseman的答案以获得2点声望:)你可以学习如何使用stackexchange [here](http://meta.stackoverflow.com/) – 2011-12-08 11:17:32

+0

@Glide - 什么是验证(它与upvoting,我做过的)。您的文档链接有点宽泛。 – 2011-12-28 15:05:59

83

要在保存到管理员后更改重定向目标,您需要覆盖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]) 
+0

我还没有说我不接受answer.It打字错误,我已经改正了它。 – ha22109 2009-08-27 10:19:58

+0

可以解释一点。我无法找到它 – ha22109 2009-08-27 10:22:50

+9

输入错误与它有什么关系?您已经在StackOverflow上提出了35个问题,并且您没有接受过单个问题的最佳答案。这是非常不礼貌的。 – 2009-08-27 10:50:54

30

要添加到@ 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) 
+0

谢谢,这真的帮了我。 – nym 2015-10-19 20:52:57

相关问题