Sergio Centeno
Sergio Centeno

Reputation: 11

Django: Form method is_valid() always return FALSE

I'm trying to make a Django app. But i have this problem. the method is_valid() always return FALSE. Maybe the problem is in the forms.py in the tipoAtributo field because when i commented it, the problem its solved. but i need to use this 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'))

Upvotes: 0

Views: 165

Answers (2)

Alasdair
Alasdair

Reputation: 308849

It doesn't make sense to use the Select widget with a MultipleChoiceField. Use SelectMultiple instead.

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

Upvotes: 0

ahmed
ahmed

Reputation: 5600

You can see why it's not valid by print errors:

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

Upvotes: 1

Related Questions