Sunday, 15 March 2015

python - Trying to set up a multiple criteria search and it is not working for me -



python - Trying to set up a multiple criteria search and it is not working for me -

i'm trying create multiple criteria search on webpage. here code have:

urls.py

from django.conf.urls import patterns, include, url app import views django.contrib import admin urlpatterns = patterns('', url(r'^profile/$',views.profile), )

views.py

def profile(request): if request.method == 'post': results = thingmodel.objects.all() th1 = request.post.get('th1', none) if th1: results = results.filter(thing1 = th1) th2 = request.post.get('th2', none) if th2: results = results.filter(thing2 = th2) th3 = request.post.get('th3', none) if th3: results = results.filter(thing3 = th3) num = request.post.get('num', none) if num: results = results.filter(number__gte = num) homecoming render_to_response('profile.html', {'results': results}) homecoming render(request,'profile.html')

models.py

class thingmodel(models.model): thing1 = models.charfield(max_length=8, blank=true) thing2 = models.charfield(max_length=8, blank=true) thing3 = models.charfield(max_length=8, blank=true) number = models.charfield(max_length=8, blank=true)

profile.html

<ul class="sidebar"> <form action="." method="post">{% csrf_token %} <li> <span>thing1:</span> <select name="th1"> <option value="selection 1">selection 1</option> <option value="selection 2">selection 2</option> <option value="selection 3">selection 3</option> </select> </li> <li><span>thing2:</span><input name="th2"></li> <li><span>thing3:</span><input name="th3"></li> <li><span>number:</span><input type="number" name="num"></li> <li><div><input type="submit" value="go!"></div></li> </form> </ul> {% if results %} {% n in results %} <table> <tr>{{n.thing1}}</tr> <tr>{{n.thing2}}</tr> <tr>{{n.thing3}}</tr> <tr>{{n.number}}</tr> </table> {% endfor %} {% else %} <p> search see results! </p> {% endif %}

please help if can - have no thought doing wrong. when click go, stays on search message.

best,

chris

what doing 1 filter clause? seek this:

field_mapping = { 'th1' : 'thing1', 'th2' : 'thing2', 'th3' : 'thing3', 'num' : 'number__gte', } kwargs = {} post_param, kwarg_field in field_mapping.items(): value = request.post.get(post_param,none) if value: kwargs[kwarg_field] = value results = thingmodel.objects.filter(**kwargs)

python django model-view-controller

No comments:

Post a Comment