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

Time:10-11

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 >
        <input  type="radio" name="password" id="Level0">
        <label  for="password">Level 0</label>
    </div>
    <div >
            <input  type="radio" name="password" id="Level1">
        <label  for="password">Level 1</label>
    </div>
        <button type="submit" >Submit</button>
</form>
<h5 >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?

CodePudding user response:

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

  • Related