我正在尝试制作一个Django应用。但我有这个问题。方法is_valid()始终返回FALSE。也许问题出在tipoAtributo字段的forms.py中,因为当我评论它时,问题就解决了。但我需要使用这个MultipleChoiceField。
forms.py
class tipoAtribute(forms.Form):
nombreAtribute = forms.CharField(max_length = 25)
CHOICES = (
('Categorico', 'Categorico'),
('NUMERICO', 'NUMERICO'))
tipoAtributo = forms.MultipleChoiceField(choices = CHOICES, required=True, widget=forms.Select())
views.py
def createTables(request):
if request.method == 'POST':
form = tipoAtribute(request.POST or None)
if form.is_valid():
print "Soy una bandera boba"
nombreAtribute = form.cleaned_data['nombreAtribute']
tipoAtributo = form.cleaned_data['tipoAtributo']
cursor = connection.cursor()
cursor.execute("use " + nombreProyecto)
cursor.execute("CREATE TABLE "+ nombreProyecto + "(prueba VARCHAR(25))")
return HttpResponseRedirect(reverse('index'))
return HttpResponseRedirect(reverse('index'))
答案 0 :(得分:1)
您可以看到打印错误无效的原因:
def createTables(request):
if request.method == 'POST':
form = tipoAtribute(request.POST) # No need for "or None"
if form.is_valid():
....
else:
print form.errors
答案 1 :(得分:0)
将Select
窗口小部件与MultipleChoiceField
一起使用是没有意义的。请改用SelectMultiple
。
tipoAtributo = forms.MultipleChoiceField(choices=CHOICES, required=True, widget=forms.SelectMultiple())