clean_data() 没有一些输入的数据
我有一个简单的表单,供用户输入姓名(CharField)、年龄(IntegerField)和性别(ChoiceField)。然而,从性别选择字段获取的数据没有显示在我的 clean_data() 中。使用调试器,我可以清楚地看到数据正在以正确的格式接收,但是一旦我执行 form.cleaned_data() ,我选择的字段数据的所有迹象都消失了。任何帮助将不胜感激。 这是相关代码:
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
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 received 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能很愚蠢,但是您是否在类上方或全局字段目录中正确格式化了 SEX_CHOICES ?
前任。
或者也许最好将 Sex 格式化为 forms.radio 小部件,这样您就可以使用单选按钮而不是下拉菜单,因为我相信选择字段默认为(如果我错了,请纠正我)
This may be silly but did you properly format SEX_CHOICES above the class or in a global fields directory?
ex.
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)
我在 POST 中使用的表单不包括性别字段,因此数据当然消失了。
The form that I was using in POST did not include the Sex field so of course the data dissappeared.