Browser back button after submittion

I have a form for adding words to the database at http://127.0.0.1:1812/angielski/add_word/. When I submit, it will redirect me to the same url, and then when I click the browser back button I will still be at the same url, but with the fields filled in. How can I do that when I click the browser back button, it redirects me to the page I was on before this one?

class AddWord(SuccessMessageMixin, LoginRequiredMixin, CreateView):
    model = Word
    fields = ['english_word', 'polish_word', 'pronunciation']
    success_url = reverse_lazy('english:add_word')
    success_message = "Words added successfully!"
path('add_word/', AddWord.as_view(), name='add_word'
<div class="card-body">
    <form method="POST" action="">
        {% csrf_token %}
        {{form}}
        <input class="button" type="submit"  value="Submit">
    </form>
</div>

You can manipulate the back button via javascript and the History API. If you’re doing full page requests with no JS, you can’t override the browser’s back button. It’s the history of the browsing session.

I would suggest you revisit the user experience you’re trying to create. Do you really want to manipulate the back button’s history or do you want to automatically redirect the user to some page after successfully processing the AddWord view’s form?

3 Likes