Saikat Mukherjee
Saikat Mukherjee

Reputation: 550

Allowing user to Submit form once in Django

i want that user can submit a particular form only once. Is it possible without any js,react.. actually by using django only ?? i have tried something like that --

def apply(request):
    p=0
    if request.method=="POST":
    p=1
     ...do something..
    else:
      ...do something...

i have tried to catch the value of p=1, and try to not return the html is it's submitted once , but each time reload makes the value of p=0. i have to save the session ?? or what will be the right way to do this ? can anyone suggest anything please?

Upvotes: 1

Views: 592

Answers (1)

Linh Nguyen
Linh Nguyen

Reputation: 3890

I suggest adding a field name form_submitted as BooleanField to your User model(by abstracting the based User model) to check and see if that user submitted the form. You can get the current logged in user from request.user.

models.py:

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    # also do this if you have custom user model
    form_submitted = models.BooleanField(default=False)
    class Meta:
        db_table = "users"

views.py:

def apply(request):
    if request.method=="POST":
       user = request.user
       if not user.form_submitted:
          # save the form
          user.form_submitted = True
          user.save()
       else:
          # this user already submitted the form

Upvotes: 1

Related Questions