有人可以解释我如何验证我的表单,它是相当简单的东西,有些我怎么可能只是没有得到它,需要验证我的选择字段,以便我可以返回并显示数据,有人可以解释如何这样做
from django import forms
from statistics.choices import MONTH_CHOICES
class StatisticsForm(forms.Form):
invoice_year = forms.CharField(max_length=255, required=False,
widget=forms.TextInput(attrs={'placeholder': 'Search Year'}))
month_choice = forms.ChoiceField(choices=MONTH_CHOICES)
答案 0 :(得分:0)
from django import forms
from statistics.choices import MONTH_CHOICES
class StatisticsForm(forms.Form):
invoice_year = forms.CharField(max_length=255, required=False,
widget=forms.TextInput(attrs={'placeholder': 'Search Year'}))
month_choice = forms.ChoiceField(choices=MONTH_CHOICES)
def clean_invoice_year(self):
invoice_year_val = self.cleaned_data.get('invoice_year')
if condition_not_satisfied:
raise forms.ValidationError('Invalid invoice year')
return invoice_year_val