代码之家  ›  专栏  ›  技术社区  ›  ha22109

管理员保存时重定向

  •  24
  • ha22109  · 技术社区  · 16 年前

    如何在保存时将用户重定向到不同的应用程序?

    我有两个应用程序,比如 app1 app2 . 如果用户单击“保存在” 附件2 然后它应该被重定向到 而不是默认页面。

    4 回复  |  直到 13 年前
        1
  •  100
  •   Alan W. Smith vishes_shell    5 年前

    要在管理员中保存后更改重定向目标,需要覆盖 response_add() (用于添加新实例)和 response_change() ModelAdmin 班级。

    请参阅中的原始代码 django.contrib.admin.options .

    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 
        app's changelist for some model
        """
        return HttpResponseRedirect(
            reverse("admin:otherappname_modelname_changelist")
        )
    
    
    def response_change(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)
    
        2
  •  37
  •   Don    9 年前

    添加到@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)
    
        4
  •  -9
  •   ha22109    16 年前

    result = super(mymodeladmin, self).change_view(request, object_id, extra_context)
    
    result['Location'] = "your location"
    
    return result
    
    推荐文章