title: Django: what is the best way to prevent multiple click on a button?
body: <p>I have a Django form with a popup confrimation before form validation.
  When validated, I save form data in a table, update 2 other tables, send an email and user is redirected to another confirmation page.</p>
  <p>It works but it may be slow in some situation.
  And in these situation, I have experienced the case a user click another time. Doing that, form data are sent again but form validation failed (normal behavior to prevent duplicate) but user is never redirected to the confirmation page and that is problematic.</p>
  <p>To fix this issue, I would like :</p>
  <ol>
  <li>to prevent user to be able to click 2 times the popup button (disable after first click)</li>
  <li>improve data process in my view</li>
  <li>improve my logic</li>
  </ol>
  <p>For the first point, I must use JS, right?
  For the second point, as 2 table are update, I think using update would be better
  For the third, ?</p>
  <p>thanks for advice</p>
  <pre><code>def randomisation_edit(request):
  
      if request.method == &quot;POST&quot;:
          form = RandomisationForm(request, data=request.POST or None)
          if form.is_valid():
              randomisation = form.save()
              pays = randomisation.ran_num[0:3].upper()
              centre = randomisation.ran_num[4:6].upper()
              status = randomisation.ran_vih
              severity = randomisation.ran_sta
              rand = Randomiser(pays, centre, status, severity)
   
              # Mise à jour de la base de données -&gt; Patient code pour rendre indisponible la ligne
              # pour une prochaine randomisation
  
              # ListeRandomisation
              # 1.Récupération de la ligne dans la liste de rando
              bras = rand['bras']
  
              # 2.Mise à jour de la ligne dans la liste de rando
              patient = get_object_or_404(ListeRandomisation, ran_ide = bras)
              patient.pat = randomisation.ran_num # pat
              patient.save()
  
              # Medicament
              # 1.Récupération de la ligne dans la liste de correspondance médicament
              medicament = rand['medicament']
  
              # 2.Mise à jour de la ligne dans la liste de correspondance médicament
              medicament = get_object_or_404(Medicament, med_ide = medicament)
              medicament.pat = randomisation.ran_num # pat
              medicament.save()
  
              pays = randomisation.ran_num[0:3].upper()
              centre = randomisation.ran_num[4:6].upper()
              request.session['can_randomize'] = is_randomizable(pays, centre)
  
              # 3. Mise à jour de la table Randomisation avec le bras et le numéro de boite attribués
              randomisation.ran_bra = patient.ran_bra
              randomisation.ran_med = medicament.med_num
              randomisation.ran_log_dat = timezone.now() # datetime.now(timezone.utc)
              randomisation.ran_log = request.user.username
              # randomisation.ran_log_sit = request.session.get('selected_site') 
              randomisation.ran_log_sit = request.session.get('user_site')
              randomisation.save()
  
              email('randomisation',randomisation.ran_num,randomisation.ran_bra,medicament.med_num,'',randomisation.ran_log,patient.ran_ide,medicament.med_ide,randomisation.ran_vih,randomisation.ran_sta)
  
              return redirect('randomization:confirmation', pk = randomisation.pk)
  
      else:
          form = RandomisationForm(request)
  
      return render(request, 'randomization/randomisation_edit.html', {'form': form})
  </code></pre>