Issue
I'm trying to redirect to an custom url as it's coded in the below. However there might be a broken url. Therefore, I like to redirect to an second url in case of error.
Is there any way to redirect to an second url in case of an error?
page = self.request.POST.get('ex_page')
return redirect(page)
Solution
If "broken URL" menas that the lookup of page
is impossible because it doesn't exist in the relevant urls.py
, then:
from django.urls.exceptions import NoReverseMatch
...
try:
destination = redirect(page)
except NoReverseMatch:
destination = redirect('homepage')
return destination
Answered By - nigel222
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.