There are URLs which I use only for redirecting a user after a successful operation. (e.g. Thank you page after submitting feedback).
Problem is that these pages can be accessed directly.
Is there a way to prevent this?
Thanks
There are URLs which I use only for redirecting a user after a successful operation. (e.g. Thank you page after submitting feedback).
Problem is that these pages can be accessed directly.
Is there a way to prevent this?
Thanks
The URL is handled by a view, so make sure the view is only happy if the last thing it did was a successful operation for the logged-in user. Otherwise raise Http404.
Just do this by checking the session variable for the user in the view. The logic goes something like:
shop.html: user hits 'post' to buy.html
view for buy.html bills the user, sets 'just bought something' in session, redirects to thanks.html
view for thanks.html checks for 'just bought something' session variable, clears it if set otherwise 404, and renders a template to the response on success.
I suggest the message framework for doing this.
You could do something very simple in the view like redirect if the the user doesn't provide a particular GET parameter:
if not request.GET.get('show', False):
# do redirection
else:
# render the page
so the URL can only be accessed with /my-url/?show=true
def get_referer(request):
referer = request.META.get('HTTP_REFERER')
if not referer:
return None
return referer
Then in any view:
def my_view(request):
if not get_referer(request):
raise Http404
return render(request, 'sample.html', {})
Here we'll be assuming that if there's no referer (None), it means the person typed in the URL into the browser.