0

I have a create view which is used to make an object of type Course(model). i'm using never_cache decorator to reload the page from server everytime i use it. now if i create a course it gets added in the Course database, now if i press browser back button it still shows me the data with course name that i wrote, which allows me to resubmit the same data, how can i prevent this?(ik i can prevent saving same data twice by overriding save method but i want to prevent user from going back to form in which he entered data).

@method_decorator(never_cache, name='dispatch')
class CourseView(CreateView):
    model = Course
    template_name = 'teacher/course.html'
    fields = ['name', 'year']
    def get_context_data(self, **kwargs):
        kwargs['course_list'] = self.model.objects.all().order_by('name','year')
        return super().get_context_data(**kwargs)

    def form_valid(self, form):
        messages.success(self.request, 'Course added successfully.') 
        return super().form_valid(form)

    def get_success_url(self):
        return self.request.GET.get(key='next', default=reverse('teacher:course'))

1 Answers1

0

never_cache works normally for requests in future. if you open CourseView again, you receive a new data.

Don't forget: When navigating browser history, it restores pages using data cached in history state of browser (requests in the past). If you really want to change result of "Back"button in browser - you should interact with history state of browser.

Maxim Danilov
  • 2,472
  • 1
  • 3
  • 8