Friday, June 28, 2019

Re: Searching with respect to gender and BY name

Hello,

I think it should work if you build the query sequentially. Like:

def search(request):
if request.method=='POST':
srch = request.POST['srh']
srchs = request.POST['gender']

if srchs or srch:
query = Snippet.objects
if srch:
query = query.filter(Q(name__istartswith=srch))
if srchs:
query = query.filter(Q(gender__iexact=srchs))

if query.count() > 0:
return render (request,'search.html',{'sr': query})
else:
messages.error(request,'no result found')
return HttpResponse("<h1>No name found please Add names </h1>")
else:
return render(request,'search.html')

return render(request,'search.html')

On Friday, 28 June 2019 15:35:09 UTC+3, Surya Adhikari wrote:
# my coding looks like this in views.py but somehow it only search by name no matter which gender i select .what am i doing wrong?

def search(request):
if request.method=='POST':
srch = request.POST['srh'] 
srchs =  request.POST['gender']

if srch and srchs:
match = Snippet.objects.filter(Q(name__istartswith=srch)) 
matchs = Snippet.objects.filter(Q(gender__iexact=srchs))

if match and matchs:
return render (request,'search.html',{'sr':match})
#return render (request,'search.html',{'sr':match})
else:
messages.error(request,'no result found')
# return HttpResponse("<h1>No name found please Add names  </h1>")

else:
# return HttpResponseRedirect('/search/')
return render(request,'search.html')

return render(request,'search.html')

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/9ebdb12b-c341-4ad4-94bd-d25e1d8372cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment