Skip to content

Instantly share code, notes, and snippets.

@cyanife
Created July 24, 2018 00:34
Show Gist options
  • Save cyanife/840c4f02c527591e48aa5aa9433c7de0 to your computer and use it in GitHub Desktop.
Save cyanife/840c4f02c527591e48aa5aa9433c7de0 to your computer and use it in GitHub Desktop.
Django redirect to original page after post

You can add a next field to your form, and set it to request.path. After you processed your form you can redirect to the value of this path.

template.html

<form method="POST">
    {% csrf_token %}
    {{ form }}
    <input type="hidden" name="next" value="{{ request.path }}">
    <button type="submit">Let's Go</button>
</form>

views.py

next = request.POST.get('next', '/')
return HttpResponseRedirect(next)
This is roughly what django.contrib.auth does for the login form if I remember well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment