Inspired by this blog post, I am trying to make a views that handles profiles searches by saving the search parameters into session so that the query can be preserved through pagination.
Here is the views:
def profile_search(request):
args = {}
qs=[]
if not request.method == 'POST':
if 'search-profiles-post' in request.session:
request.POST = request.session['search-profiles-post']
request.method = 'POST'
if request.method == "POST":
form = AdvancedSearchForm(request.POST)
request.session['search-profiles-post'] = request.POST
if form.is_valid():
cd = form.cleaned_data
s_country=cd['country']
s_province=cd['province']
s_city = cd['city']
if s_city: qs.append( Q(city__in=s_city))
if s_country: qs.append(Q(country__icontains = s_country))
if s_province: qs.append( Q(province__icontains=s_province))
f = None
for q in qs:
if f is None:
f=q
else: f &=q
print f
if f is not None:
profiles = UserProfile.objects.filter(f).order_by('-created_at')
else:
form = AdvancedSearchForm()
profiles = UserProfile.objects.all().order_by('-created_at')
paginator = Paginator(profiles,12)
page= request.GET.get('page')
try:
results = paginator.page(page)
except PageNotAnInteger:
results = paginator.page(1)
except EmptyPage:
results = paginator.page(paginator.num_pages)
args.update(csrf(request))
args['form'] = form
args['results'] = results
return render_to_response('userprofile/advanced_search.html', args,
context_instance=RequestContext(request))
Here is the form:
<form action="/search/" method="post">{% csrf_token %}
<ul class="list-unstyled">
<li><h3>Country</h3></li>
<li>{{form.country}}</li><br>
<h4>Province</h4>
<li>{{form.province}}</li>
<h4>City</h4>
<li>{{form.city}}</li>
</ul>
<input type="submit" name="submit" value="search" />
</form>
Search Results:
{% for p in results %}
<div">
<div>
<br>
<strong><a href="/profile/{{p.username}}" >{{p.username}}</a></strong>
{{p.country}} <br>
{{p.province}} <br>
{{p.city}} <br>
</div>
</div>
{% endfor %}
<div>
<div class="pagination">
{% if results.has_previous %}
<a href="?page={{ results.previous_page_number }}"> << Prev </a>  
{% endif %}
{% if results.has_next %}
<a href="?page={{ results.next_page_number }}"> Next >> </a>
{% endif %}
</div>
</div>
</div>
and the urls.py
url(r'^search/', 'userprofile.views.profile_search'),
This is the mysterious error theat I get:
Traceback:
File "/home/supermario/.djenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
204. response = middleware_method(request, response)
File "/home/supermario/.djenv/local/lib/python2.7/site-packages/debug_toolbar/middleware.py" in process_response
89. new_response = panel.process_response(request, response)
File "/home/supermario/.djenv/local/lib/python2.7/site-packages/debug_toolbar/panels/request.py" in process_response
31. 'post': [(k, request.POST.getlist(k)) for k in sorted(request.POST)],
Exception Type: AttributeError at /search/
Exception Value: 'dict' object has no attribute 'getlist'
I got stock on this for a while so really appreciate your hints.
request.POST
should be the dict-like QueryDict
but not the simple python dict
:
from django.http import QueryDict
request.POST = QueryDict('').copy()
request.POST.update(request.session['search-profiles-post'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With