Reputation: 3
I passed the id value from html form to views .py. I want to check if the value matches with the one in database. But for some reason it's not working.
list.html
<form method= 'POST' action = "{% url 'jobs:pinned'%}">
{% csrf_token%}
<input type ="text" name = "number">
<input type="submit" value="Submit">
</form>
views.py
def pinned(request,category_slug=None):
users = User.objects.exclude(id=request.user.id)
jobs_list1 = Jobs.objects.all()
if request.method =="POST":
vari = request.GET.get('number')
for evert in jobs_list1:
if evert.Job_Id == vari:
evert.flag = True
evert.save(update_fields=["flag"])
context = {
'job_list1':jobs_list1,
'users':users
}
return render(request, 'jobs/product/list.html',context)
Here, if i put a static value as 511, i.e if evert.Job_Id ==511
, it works. But if i change it to request.GET.get('number'), it's not working. How do i send value from form input value to views.py. Thanks.
Upvotes: 0
Views: 512
Reputation: 3
It turns out, I was comparing string with an integer. Thus I solved this problem as:
if request.method =="POST":
vari = request.POST.get('number')
vari = int(vari)
for evert in jobs_list1:
vari1 = evert.Job_Id
.....
Upvotes: 0
Reputation: 5854
Firstly method of your form is POST, so for GET method it will work.
for post method try this
vari = request.POST.get('number')
hope it helps
Upvotes: 0