django - GET url in search and sort logic -
i have url after hit search button:
127.0.0.1:8000/results/?name=blab&city=bla&km=12 my view:
def search(request): name = request.get.get('name') city = request.get.get('city') km = request.get.get('km') if name!="" , name != none: locations = location.objects.filter(name__istartswith=name) return render_to_response("result-page.html",{'locations':locations},context_instance=requestcontext(request)) if city!="" , city != none: locations = location.objects.filter(city__istartswith=city) return render_to_response("result-page.html",{'locations':locations},context_instance=requestcontext(request)) but now, if both name , city, giving ony results search after name. e.g. first paramater. second 1 not being taken.
what best logic this? want able sort search result. can please give me hints how kind of things in clean logic.
thanks
you returning on first if, if want filter on either or both or no parameters try using 1 queryset dynamic filters e.g.
search_kwargs = {} if request.get.get('name'): search_kwargs['name__istartswith'] = request.get.get('name') if request.get.get('city'): search_kwargs['city__istartswith'] = request.get.get('city') locations = location.objects.filter(**search_kwargs) return render_to_response("result-page.html",{'locations':locations},context_instance=requestcontext(request)) or
filter_fields = ['city','name'] f in filter_fields: if f in request.get: search_kwargs['%s__istartswith' % f] = request.get.get(f)
Comments
Post a Comment