Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

django - Redirect on admin Save

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...