Good morning. I could not solve this error "local variable" - 'referenced before assignment ", my code is this:
Vista:
def palcosSearch(request):
if request.method == 'POST':
form = SearchPalco(request.POST)
if form.is_valid():
NamePalco = form.cleaned_data('palco')
palcos = Palco.objects.filter(palcoName=NamePalco)
else:
form=SearchPalco()
palcos=None
return render(request,'ExamenTemplates/palcosSearch.html',{"form":
form,"palcos":palcos})
Model
class Palco(models.Model):
Category = models.CharField(max_length = 255)
palcoName = models.CharField(max_length = 255)
capacity = models.IntegerField()
location = models.CharField(max_length = 255)
class Meta:
db_table = "Palco"
def __str__(self):
return self.palcoName
Form
class SearchPalco(forms.Form):
palco = forms.ModelChoiceField(label='Palco',initial='',
widget=forms.Select(),
queryset=None)
def __init__(self,*args,**kwargs):
super(SearchPalco,self).__init__(*args,**kwargs)
self.fields['palco'].widget.attrs['class']= 'form-control'
self.fields['palco'].widget.attrs['placeholder']= 'Ingrese palco.'
self.fields['palco'].queryset =
Palco.objects.values_list('palcoName', flat=True)
I know the reason is that the condition does not pass in is_valid()
view but I can not find why.