[Django]-Django: Is there a way to prevent a view from being called twice simultaneously?

4👍

You should use POST request and redirect after it, really.

Also, to prevent double submitting, you can employ a solution like one from this question: Does a library to prevent duplicate form submissions exist for django?. You can store a key refreshing_vcs in request.session and check its value in your view. If the data is already being refreshed, then you can redirect user to another page and tell to wait a bit.

Completely another way would be to setup a django-celery task (or simply use cron) to perform updates at regular intervals automatically, but I don’t know whether it suit your requirements.

0👍

from django.views import View
from django.shortcuts import render, redirect 

class MyView(View):
    def get(self, request):
         return render(request, 'get.html')
    def post(self, request):
        # handle your post here
        # maybe you had a form that was filled, handle it here
        # then instead of returning a HttpResponse
        # use a redirect, that way, a refresh won't result in the 
        # post method been called twice. 
        # note if you want, you can also safely redirect to the same view
        return redirect('/my-login-view')

looking at the django code the redirect functions arguments can be:
1. A model
2. A view name or
3. A URL

👤Komu

-1👍

That is the best solution, however you could disable the button onclick by adding a disabled attribute to the button equalling ‘true’

With jQuery:
$(‘#yourButtonId’).attr(“disabled”, true);

Leave a comment