django Query filter

49 views
Skip to first unread message

Abhineet Baranwal

unread,
Apr 3, 2019, 11:33:20 AM4/3/19
to Django users
this is my views file which creates an error like  django.utils.datastructures.MultiValueDictKeyError: 'search1' . Can anyone tell me where i go wrong?

def viewstr(request):
    views = create_store.objects.all()
    brn = Prod_Brand.objects.all()
    mi = ''
    if request.method == 'POST':
        # mi = ''
        search1 = request.POST['search1']
        if search1 == '' :
            pass
        else:
            mi = create_store.objects.filter(Q(select_brand__Brand__icontains=search1))
            if mi:
                return render(request,'viewstr.html',{'vi':mi,'brn':brn})
        search = request.POST['search']
        if search == '' :
            pass
        else :
            mi = create_store.objects.filter(Q(id__icontains=search))
            if mi:
                return render(request,'viewstr.html',{'vi':mi,'brn':brn})

    return render(request,'viewstr.html',{'viewstr':viewstr,'vi':mi,'brn':brn})

Guru Murthy

unread,
Apr 3, 2019, 11:47:22 AM4/3/19
to django...@googlegroups.com
Hi Abhijeet baranwal, 
  I think you are adding contains is not correct query listing you have to use in query instead


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cdafd13a-46b5-471d-a927-b3d5d8e58f5c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Abhineet Baranwal

unread,
Apr 3, 2019, 12:02:00 PM4/3/19
to django...@googlegroups.com
Thanks for your efforts, But i didn't get it can you please elaborate your answer? 

dvij parekh

unread,
Apr 3, 2019, 12:27:02 PM4/3/19
to django...@googlegroups.com

seems like search1 key is containg multi value dictionary and you are checking it with == which is for single value
after          
search1 = request.POST['search1']      
put  
print( search1  )
return "hello"

 and comment code remaining below ,check terminal it will show you how many value you have in dictionary 


--

Guru Murthy

unread,
Apr 3, 2019, 12:32:59 PM4/3/19
to django...@googlegroups.com
Hi abhineet baranwal,
You have to use q(id__in=search) instead q(id__icontains=search) ..search more about how to use in query in django and 1 thing you are not getting correct post value of searc1

Nikolay Smirnov

unread,
Apr 3, 2019, 4:55:20 PM4/3/19
to Django users
Your request.POST does not contain "search1".
Do so search1 = request.POST.get('search1')

Example:
def viewstr(request):
    views = create_store.objects.all()
    brn = Prod_Brand.objects.all()
    mi = ''
    if request.method == 'POST':
        search1 = request.POST.get('search1')
        if search1:
            mi = create_store.objects.filter(Q(select_brand__Brand__icontains=search1))
            if mi:
                return render(request,'viewstr.html',{'vi':mi,'brn':brn})
        search = request.POST.get('search')
        if search:

Abhineet Baranwal

unread,
Apr 4, 2019, 7:06:16 AM4/4/19
to django...@googlegroups.com
I edited it as you suggested but it is giving another error like 'method' object is not subscriptable

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Abhineet Baranwal

unread,
Apr 4, 2019, 7:09:52 AM4/4/19
to django...@googlegroups.com
Thanks..I'll search for that but i think its not related to the query, its related to two search i have used in one post request

Abhineet Baranwal

unread,
Apr 4, 2019, 7:43:51 AM4/4/19
to django...@googlegroups.com
Thanks for the help, this is showing only one value in dictionary at a time . There is no fault here but i have a question can we run multiple search query in same page with different id ? And it shows that error even now

Message has been deleted

Nikolay Smirnov

unread,
Apr 4, 2019, 3:05:17 PM4/4/19
to Django users
The global POST variable allows you to access the data sent with the POST method by input name. See more info https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_and_retrieving_form_data.

Your html:
<form action=".">
  First name:<br>
  <input type="text" name="search">
  <br>
  Last name:<br>
  <input type="text" name="search1">
  <br><br>
  <input type="submit" value="Submit">
</form>

On Wednesday, 3 April 2019 13:33:20 UTC+2, Abhineet Baranwal wrote:
Reply all
Reply to author
Forward
0 new messages