django - Create a query to database from the data in form -
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 form history = form.cleaned_data['history_contract']#data form category = form.cleaned_data['category']#//data form query = sotrudniki.objects.filter(fio__contain=name, history_contract__contain=history, category__contain=category)#the part want accomplish else: form = nameform() return render(request, 'personal.html', {'form': form, 'query': query})
i need check data entered form. after making request database , returning data indicated in form. got error @ end "local variable 'query' referenced before assignment", maybe have wrong approach task. how can implement idea?
this simple logic error. query
not exist when form not post, try , send template anyway. can fix putting query = none
@ beginning of function.
Comments
Post a Comment