32

How can I redirect a user to different app on Save?

I have two app, say app1 and app2. If a user clicks on Save in app2 then it should be redirected to app1 rather then the default page.

I don't want to do a customform.

3
  • 7
    Why no accepted answers for any questions? Aug 27, 2009 at 9:41
  • On save where? In the admin, in a custom form, where? Aug 27, 2009 at 9:50
  • in admin not in custom form.it is simple admin, i am overriding the save function
    – ha22109
    Aug 27, 2009 at 10:01

5 Answers 5

105

To change the redirect destination after save in the admin, you need to override response_add() (for adding new instances) and response_change() (for changing existing ones) in the ModelAdmin class.

See the original code in django.contrib.admin.options.

Quick examples to make it clearer how to do this (would be within a ModelAdmin class):

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)
6
  • i havent said that i am not accepting the answer.It was typing mistake and i have corrected it.
    – ha22109
    Aug 27, 2009 at 10:19
  • can u explain it little more .I am not able to find it out
    – ha22109
    Aug 27, 2009 at 10:22
  • 11
    What does a typing error have to do with it? You have asked 35 questions on StackOverflow, and you haven't accepted a best answer for a single one. That is very bad manners. Aug 27, 2009 at 10:50
  • i am not aware of this that we have to accept answer manually.
    – ha22109
    Aug 27, 2009 at 11:02
  • Daniel's answer helped me - I'd just mention that a user might also want to take into account '_addanother', i.e. check that neither '_continue' nor '_addanother' are in request.POST before choosing to redirect back out of admin to one's app.
    – Paul J
    Jul 10, 2015 at 22:48
44

To add to @DanielRoseman's answer, and you DON'T want the user redirected when they choose Save and continue and not the Save button, then you could use this solution instead.

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)
5
  • Thanks, this really helped me out.
    – nym
    Oct 19, 2015 at 20:52
  • @dan-mantyla this works lovely for me but do you happen to know how to stop the redirect on save and add another? Thanks Jan 20, 2019 at 7:35
  • @Davies-Barnard its been a long time since I've worked with Django. Maybe this could be helpful: stackoverflow.com/questions/14126371/… Jan 28, 2019 at 15:34
  • 1
    Thanks @DanMantyla. Its actually a lot simplier but I couldn't see wood for the trees. The name of the Save and Continue button is _continue and likewise the name of the Save and Add Another button is _addanother I just needed to adapt the code above to distinguish between these values. Jan 29, 2019 at 9:22
  • Django Admin also supports _saveasnew save button (displayed if save_as=True is added to your ModelAdmin class), so you should also handle it just as you handle _continue. Jul 1, 2019 at 17:32
2

Given the previous answers I would suggest to override response_post_save_add and response_post_save_change methods. So you don't skip django's default logic in response_add and response_change

0

Maybe take a look at URL namespaces... You could probably use HttpResponseRedirect + reverse to redirect the user inside of your overridden save method. Keep in mind that this is a new feature in Django 1.1 and is not supported in 1.0.x.

http://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces

-9

def change_view(self, request, object_id,extra_context=None):

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

result['Location'] = "your location"

return result
5
  • 5
    This is not a great answer: what happens if the change_view is unsuccessful? Also assigning to result['Location'] is not very django-like (even if it may work). The answer above (by Daniel Roseman) is a good one. Nov 8, 2011 at 18:02
  • You should validate Roseman's answer to get 2 points of reputation :) You can learn how to use stackexchange here Dec 8, 2011 at 11:17
  • @Glide -- what's validating (is it distinct from upvoting, which I did do). Your docs link to was a bit broad. Dec 28, 2011 at 15:05
  • @Glide still not following what you mean by "validating" (or maybe I misinterpreted your comment - perhaps you were talking to the OP, not me...) Jan 3, 2012 at 12:55
  • 1
    Good question, bad answer.
    – Gocht
    Sep 10, 2015 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.