cleaned_data() doesn't have some of the entered data
I have a simple form for a user to enter in Name (CharField), Age(IntegerField), and Sex(ChoiceField). However the data that is taken from the Sex choice field is not showing up in my cleaned_data(). Using a debugger, I can clearly see that the data is being rece开发者_如何学Goived in the correct format but as soon as I do form.cleaned_data() all sign of my choice field data is gone. Any help would be greatly appreciated. Here is the relative code:
class InformationForm(forms.Form):
Name = forms.CharField()
Age = forms.IntegerField()
Sex = forms.ChoiceField(SEX_CHOICES, required=True)
def get_information(request, username):
if request.method == 'GET':
form = InformationForm()
else:
form = RelativeForm(request.POST)
if form.is_valid():
relative_data = form.cleaned_data
This may be silly but did you properly format SEX_CHOICES above the class or in a global fields directory? ex.
SEX_CHOICES = (
('M', 'Male'),
('F', 'Female'),
('O', 'Other'),
)
Or maybe it's best to format Sex as a forms.radio widget, so you have radio buttons instead of a drop-down as i believe the choicefield defaults to (correct me if i'm wrong)
The form that I was using in POST did not include the Sex field so of course the data dissappeared.
精彩评论