Django:Form方法is_valid()始终返回FALSE

2024-09-29 23:18:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个Django应用程序。但我有这个问题。方法是\ u valid()始终返回FALSE。也许问题出在表单.py在tipoAtributo领域,因为当我评论它时,问题解决了。但我需要用这个多重冰场。你知道吗

在表单.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())

在视图.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'))

Tags: pyform表单ifrequestformspostcursor
2条回答

您可以通过打印错误了解它无效的原因:

def createTables(request):
 if request.method == 'POST':
    form = tipoAtribute(request.POST) # No need for "or None"
    if form.is_valid():
        ....
    else:
        print form.errors

Select小部件与MultipleChoiceField一起使用是没有意义的。改用^{}。你知道吗

tipoAtributo = forms.MultipleChoiceField(choices=CHOICES, required=True, widget=forms.SelectMultiple())

相关问题 更多 >

    热门问题