开发者

What if success_url is irrelevant with the model itself in Django

开发者 https://www.devze.com 2023-03-16 07:35 出处:网络
If the success_url is determined by the model, we can customize the generic view as below. def get_success_url(self):

If the success_url is determined by the model, we can customize the generic view as below.

def get_success_url(self):
    return '/object/%s' % (self.object.id)

But if the success_url is irrelevant with the model, suc开发者_如何学运维h as, we want to return to the page before we entered the form page. It can be a detail page, a list page, or anything else. Can I store the referral url in the form? Any idea is appreciated.

~Hanson


I assume that the success page will not always be the same as otherwise you could just include the url with something like reverse() in get_success_url. For a dynamic redirect you could either, like you suggested add the url in a hidden input to the form or in the querystring of the url the form is posted to (eg. /myurl?next=/redirect-url/). Then you could do in your view class something like:

# url is in the form in a hidden input 'next'
def get_success_url(self):
    return request.POST['next']

# url is in the query string ?next=xxxx
def get_success_url(self):
    return request.GET['next']
0

精彩评论

暂无评论...
验证码 换一张
取 消