Reputation: 9
I would like to only take input of A or B from HTML Form I create... But this one did not work. I have tried if type is not 'A' or 'B' or using !=.. nothing worked. What is the right code for it?
forms.py
class YamlForm(forms.ModelForm):
class Meta:
model = Form_info
fields = '__all__'
# fields = ['name', 'type', 'vlan_id', 'rdp', 'ip_master', 'ip_backup', 'vip', 'nat_ip', 'cidr', 'dns', 'dhcp', 'dhcp_pool_start', 'dhcp_pool_end']
widgets = {
'name': forms.TextInput(attrs={'id': 'name', 'name': 'name'}),
'vlan_id': forms.TextInput(attrs={'id': 'vlan_id', 'name': 'vlan_id'}),
'rdp': forms.Select(choices=BOOLEAN_CHOICES),
'dhcp':forms.Select(choices=BOOLEAN_CHOICES)
}
error_messages = {
'name': {
'max_length': _("This writer's name is too long."),
},
}
def clean_type(self):
type = self.cleaned_data['type']
if not 'A' or 'B':
raise forms.ValidationError("Please enter A or B")
return type
def clean_vlan_id(self):
vlan_id = self.cleaned_data.get('vlan_id')
if vlan_id not in range(1300, 1899):
raise forms.ValidationError("Pleanse enter the right # in range")
return vlan_id
my HTML
</div>
<div class="row">
<div class="col-25">
<label for="type">TYPE:</label>
</div>
<div class="col-75">
<span class="text-danger" small>{{ form.type.errors }}</span>
{{ form.type }}
</div>
Upvotes: 0
Views: 1035
Reputation: 9977
Your problem is not with Django, but purely with Python.
if not 'A' or 'B':
won't evaluate to what you expect. Instead when you want to compare a variable on multiple values use this syntax:
if type not in {'A','B'}:
Note that I used a set
be cause it has a faster look-up time than other data structures like list
.
Upvotes: 2