0

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?

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.