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.

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
ha22109
  • 8,036
  • 13
  • 44
  • 48

5 Answers5

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)
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • i havent said that i am not accepting the answer.It was typing mistake and i have corrected it. – ha22109 Aug 27 '09 at 10:19
  • can u explain it little more .I am not able to find it out – ha22109 Aug 27 '09 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. – Daniel Roseman Aug 27 '09 at 10:50
  • i am not aware of this that we have to accept answer manually. – ha22109 Aug 27 '09 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 '15 at 22:48
  • @ha22109 You should see a grey checkmark to the left of each answer, underneath the rank up/down arrows (The checkmark only appears on answers to questions you have asked). To accept an answer, you click the checkmark and it turns green. This helps other folks who have the same question know which answer to look at first. Edit: Hmm, I see now that you have accepted an answer below. I wrote this in response to your reply to Daniel's comment above thinking you hadn't accepted any answer. I'll leave it anyway just in case. – jenniwren Nov 09 '15 at 20:11
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)
Don
  • 16,928
  • 12
  • 63
  • 101
Dan Mantyla
  • 1,840
  • 1
  • 22
  • 33
  • Thanks, this really helped me out. – nym Oct 19 '15 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 – Byte Insight Jan 20 '19 at 07:35
  • @Davies-Barnard its been a long time since I've worked with Django. Maybe this could be helpful: https://stackoverflow.com/questions/14126371/in-django-how-to-override-the-save-and-continue-feature/14126372 – Dan Mantyla Jan 28 '19 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. – Byte Insight Jan 29 '19 at 09: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`. – Eerik Sven Puudist Jul 01 '19 at 17:32
1

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

Evgeni Shudzel
  • 231
  • 2
  • 5
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

Jon Mooring
  • 151
  • 2
  • 13
-8

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
ha22109
  • 8,036
  • 13
  • 44
  • 48
  • 4
    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. – Tim Diggins Nov 08 '11 at 18:02
  • You should validate Roseman's answer to get 2 points of reputation :) You can learn how to use stackexchange [here](http://meta.stackoverflow.com/) – Pierre de LESPINAY Dec 08 '11 at 11:17
  • @Glide -- what's validating (is it distinct from upvoting, which I did do). Your docs link to was a bit broad. – Tim Diggins Dec 28 '11 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...) – Tim Diggins Jan 03 '12 at 12:55
  • Good question, bad answer. – Gocht Sep 10 '15 at 17:04