clean_data() 没有一些输入的数据

发布于 2024-09-05 04:10:29 字数 638 浏览 3 评论 0原文

我有一个简单的表单,供用户输入姓名(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2024-09-12 04:10:29

这可能很愚蠢,但是您是否在类上方或全局字段目录中正确格式化了 SEX_CHOICES ?
前任。

SEX_CHOICES = (
    ('M', 'Male'),
    ('F', 'Female'),
    ('O', 'Other'),
)

或者也许最好将 Sex 格式化为 forms.radio 小部件,这样您就可以使用单选按钮而不是下拉菜单,因为我相信选择字段默认为(如果我错了,请纠正我)

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)

巾帼英雄 2024-09-12 04:10:29

我在 POST 中使用的表单不​​包括性别字段,因此数据当然消失了。

The form that I was using in POST did not include the Sex field so of course the data dissappeared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文