Flask: redirect to same page after form submission
This worked perfectly for me, in last line:
return redirect(request.referrer)
Change form action to action="{{url_for('delete_images')}}"
. And for redirection you can use code below:
@app.route('/delete', methods=['POST'])
def delete_images():
return redirect(url_for('delete_images'))
As archer said below:
return redirect(request.referrer)
This is useful when you have a button that uses a route to perform a given function when it is clicked - you don't want to return the user to the URL for that button - you want to return the user to the URL that the button route was referred by, i.e. the page the user was on when they clicked the button.
However, as Mahmoud said:
redirect(request.url)
This is perfect if you perform a function on a page that doesn't use routes or special URLs or anything like that. It essentially just refreshes the page.
You can get the currently requested URL by request.url
:
So, to redirect to the same page use:
redirect(request.url)