Reputation: 55
I want to pass a GET parameter when rendering a view in Django. My code is as follows :
class CustomerList(ListView):
model = Customer
template_name = 'management/customer_list.html'
def get(self, request):
search_text = request.GET.get('search_text', '') # check if search_text exists
query = Q(name__icontains=search_text) | Q(phone_number__icontains=search_text)
object_list = self.model.objects.filter(query).order_by('-regdate', '-id')
if object_list.count() == 0:
response = redirect('management:customer-create')
response['Location'] += '?name=%s' % (search_text)
return response
context = {
'object_list':object_list,
'search_text':search_text,
'count':self.model.objects.count()
}
return render(request, self.template_name, context)
and the resulting url goes like this:
/customer/=?utf-8?b?L2N1c3RvbWVyL2NyZWF0ZS8/bmFtZT3tmY3quLjrj5k=?=
with page not found error
What do you think am I doing wrong?
Upvotes: 0
Views: 437
Reputation: 55
From the comment I found the solution, which was to use urlencode().
if object_list.count() == 0:
from django.http import QueryDict
response = redirect('management:customer-create')
q = QueryDict(mutable=True)
q['name'] = search_text
query_string = q.urlencode() # encodes utf8 string also
response['Location'] += '?%s' % (query_string)
return response
Upvotes: 1