2016-04-10 24 views
0

Bir Django uygulaması yapmaya çalışıyorum. Ama bu problemim var. yöntem is_valid() daima FALSE döndürür. Belki de problem, tipoAtributo alanındaki forms.py'dir, çünkü yorum yaptığımda sorun çözülmüştür. ama bu MultipleChoiceField'ı kullanmam gerekiyor.Django: Form yöntemi is_valid() her zaman FALSE döner

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')) 

cevap

1

o baskı hataları geçerli değil sebebisiniz görebilirsiniz: Kullanımı mantıklı değil

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

MultipleChoiceField ile Select tarihinde eklendi. Bunun yerine SelectMultiple kullanın.

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