Kaiss Bouali
Kaiss Bouali

Reputation: 105

checking radio button on Django view from template

I am creating a custom sign up form and this is the view I created:

def signup(request):
    if request.user.is_authenticated:
        return render(request, 'listings/index.html', {})

    if request.method == 'POST':
        if 'cr-1' in request.POST:
            newuser = User.objects.create_user(username=request.POST.get('email'), email=request.POST.get('email'), password=request.POST.get('password'))
            newpatient = Patient.objects.create(user=newuser)
            user = authenticate(request, username=username, password=password)
            if user is not None:
                auth_login(request, user)
                return redirect('dashboard')
        elif 'cr-2' in request.POST:
            newuser = User.objects.create_user(username=request.POST.get('email'), email=request.POST.get('email'), password=request.POST.get('password'))
            newdoctor = Doctor.objects.create(user=newuser)
            user = authenticate(request, username=username, password=password)
            if user is not None:
                auth_login(request, user)
                return redirect('dashboard')
        else:
            print("##################### NOTHING HAPPENED #####################")

    return render(request, 'dashboard/signup.html', {})

I purposely added an else statement printing NOTHING HAPPENED on the console because nothing happens when I click on submit. Here is the template:

<form class="form-type-material" method="POST" action="{% url 'signup' %}">
            {% csrf_token %}

            <div class="custom-control custom-radio">
              <input type="radio" id="cr-1" name="rg-1" class="custom-control-input" checked>
              <label class="custom-control-label" for="cr-1">Patient/Visiteur</label>
            </div>

            <div class="custom-control custom-radio">
              <input type="radio" id="cr-2" name="rg-1" class="custom-control-input">
              <label class="custom-control-label" for="cr-2">Médecin/Praticien</label>
            </div>

            <div class="form-group">
              <input type="text" class="form-control" id="id_email" name="email" required>
              <label for="email">Adresse email</label>
            </div>

            <div class="form-group">
              <input type="password" class="form-control" id="id_password" name="password" required>
              <label for="password">Mot de passe</label>
            </div>

            <div class="form-group">
              <input type="password" class="form-control" id="id_password-conf" name="password-conf" required>
              <label for="password-conf">Confirmation du mot de passe</label>
            </div>

            <div class="form-group">
              <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" required>
                <label class="custom-control-label">I agree to all <a class="text-primary" href="#">terms</a></label>
              </div>
            </div>

            <br>
            <button class="btn btn-bold btn-block btn-primary" type="submit">Créer et accéder au tableau de bord</button>
          </form>

What seems to be the issue?

PS: In a previous attempt I did if 'cr-1'.checked in request.POST because I saw it in another stackoverflow thread but it gave me the error: str .... has no checked method.

Upvotes: 0

Views: 1115

Answers (1)

Adaikalaraj
Adaikalaraj

Reputation: 498

Here in your template you have 'cr-1' and 'cr-2' as ids, but while submitting the form the name will be sent. i.e here you have name name="rg-1" So, you need to check

if 'rg-1' in request.POST:

Upvotes: 2

Related Questions