Reputation: 1035
My code
#model.py
class Personal(models.Model):
name = models.CharField(db_column='FIO', max_length=255, blank=True, null=True)
history_contract = models.CharField(max_length=255, blank=True, null=True)
category = models.CharField(max_length=255, blank=True, null=True)
#form.py
class NameForm(forms.Form):
name = forms.CharField(label='Name', max_length=100)
history_contract = forms.CharField(label='History', max_length=100)
category = forms.CharField(label='Category', max_length=100)
#view
def get_personal(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']#data from form
history = form.cleaned_data['history_contract']#data from form
category = form.cleaned_data['category']#//data from form
query = Sotrudniki.objects.filter(fio__contain=name, history_contract__contain=history, category__contain=category)#The part I want to accomplish
else:
form = NameForm()
return render(request, 'personal.html', {'form': form, 'query': query})
I need to check the data that I entered into the form. After making a request to the database and returning only the data that I indicated in the form. I got an error at the end "local variable 'query' referenced before assignment", maybe I have a wrong approach to the task. How can I implement the idea?
Upvotes: 0
Views: 66
Reputation: 599610
This is a simple logic error. query
does not exist when the form is not a POST, but you try and send it to the template anyway. You can fix this by putting query = None
at the beginning of the function.
Upvotes: 0