简体   繁体   English

如何验证Django 1.7中的下拉菜单?

[英]How do you validate a dropdown in Django 1.7?

I have a Django Form that doesn't record the Dropdown value when I create a validation. 创建验证时,我有一个不记录Dropdown值的Django表单。 Instead the validation reads the value as always being null. 相反,验证将读取的值始终为null。 This thus triggers the validation: 因此,这触发了验证:

This field cannot be null.

What could I be doing wrong? 我可能做错了什么? I am new to the Django framework, so any help would be appreciated. 我是Django框架的新手,所以将不胜感激。 Here is my forms.py file: 这是我的forms.py文件:

from django import forms 
from reviews.models import Rating, Review

CHOICES = (
    (1,'One Star'),
    (2,'Two Star'),
    (3,'Three Star'),
    (4,'Four Star'),
    (5,'Five Star')
)

SERVICES = (
(0, 'Select a Service Name or Start typing'),
(85, 'Service1'),
(86, 'Service2'),
(88, 'Service3'),
(89, 'Service4'),
(90, 'Service5')
)

class ReviewForm(forms.ModelForm):
    serv_id = forms.ChoiceField(required=True, choices= SERVICES, widget=forms.Select(attrs={'class':'form-control'}))
    rating_value = forms.ChoiceField(required = True, choices = CHOICES,  widget=forms.RadioSelect(attrs={'class' : ''}))
    review_text = forms.CharField(required=True, widget=forms.Textarea(attrs={'class' : 'form-control', 'placeholder': 'Write Review', 'rows':5}))

    class Meta:
        model = Review
        fields = ('rating_value', 'review_text', 'serv_id',)

    def clean_serv_id(self):
        serv_id = self.cleaned_data['serv_id']
        if serv_id <= 0:
            raise forms.ValidationError("You need to select a Service.")

As long as a user selects an option from the drop down (id integer value) it works. 只要用户从下拉列表中选择一个选项(id整数值),它就起作用。 The problem is that the validation doesn't get triggered when the id = 0 (the user doesn't select a service): 问题是,当id = 0(用户未选择服务)时,不会触发验证:

You need to return the value from clean_serv_id . 您需要从clean_serv_id返回值。

def clean_serv_id(self):
    serv_id = self.cleaned_data['serv_id']
    if serv_id <= 0:
        raise forms.ValidationError("You need to select a Service.")
    else:
        return serv_id

I was going to post this a lot earlier.. The issue was that my comparison operator in my validation was incorrect-- oh and I definitely needed to return serv_id, I just removed it from the original code after trying to debug for some reason.. Apparently, the numerical value 0 that I used as a value in my widget is interpreted as "0" in the ChoiceField. 我打算早些发布它。问题是验证中的比较运算符不正确-哦,我确实需要返回serv_id,出于某种原因尝试调试后,我将其从原始代码中删除了。显然,我在我的小部件中用作值的数值0在ChoiceField中被解释为“ 0”。

Here is the validation that works. 这是有效的验证。 Notice that I also had to change the comparison operator to "==": 注意,我还必须将比较运算符更改为“ ==”:

def clean_serv_id(self):
        serv_id = self.cleaned_data['serv_id']
        if serv_id == "0":
            raise forms.ValidationError("You need to select a Service.")
        else:
            return serv_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM