Hi My Django Forms.cleaned_data is not giving me any output. Can someone help me?

Question:

I am trying to create a page to generate passwords and the user will select either level 0 or level 1 for varying strengths of the password. And I am not able to get the users selection of level 0 or level 1 using the radio button.

My views.py

from django.shortcuts import render
import random
import string
from types import NoneType
from random_word import RandomWords
from .forms import CHOICES

def password(request):
    passw = passGenAdvanced()
    form = CHOICES(request.POST)
    if form.is_valid():
        selected = form.cleaned_data.get("password")
        print(selected)

    return render(request, 'generator/password.html', {"password":passw})

My forms.py

from django import forms
password = [('0','Level 0'), ('1', 'Level 1')]
class CHOICES(forms.Form):
    password = forms.ChoiceField(choices=password, widget=forms.RadioSelect)

My html file

{% extends 'generator/base.html'%}
{% block content %}
<style>
    .button {
      background-color: #4CAF50;
      border: none;
      color: white;
      padding: 15px 32px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
      margin: 4px 2px;
      cursor: pointer;
    }
</style>
<form method = "POST">
    {% csrf_token %}
    <div class="form-check">
        <input class="form-check-input" type="radio" name="password" id="Level0">
        <label class="form-check-label" for="password">Level 0</label>
    </div>
    <div class="form-check">
            <input class="form-check-input" type="radio" name="password" id="Level1">
        <label class="form-check-label" for="password">Level 1</label>
    </div>
        <button type="submit" class="button">Submit</button>
</form>
<h5 class="alert alert-success">Your Generated Password: <strong>{{password}}</strong></h5> 
{% endblock %}

Sorry if the problem may seem obvious, I am new to django.

After printing out the form.error it keeps saying this "on" it isn’t even one of my radio options. So how does it give me that?

Asked By: James Bond

||

Answers:

The radio inputs are missing the value so add value for each radio and try again.

Answered By: Mohamed ElKalioby
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.