tags:

views:

53

answers:

1

I am using Django.

In a regular form, the user enters "Gerry & Pacemakers".

(Notice the Ampersand sign.)

When I go views.py...

def myview(request):
    q = request.GET.get('q','').strip()
    print q

q is "Gerry"...but it's supposed to be "Gerry & Pacemakers"...encoded Is the correct way doing this by using urllib??

How do I encode it BEFORE it hits the view? It's very weird, because the URL contains the encoding: ?q=gerry+%26+pacemakers

+2  A: 

Since you are pulling the data from request.GET, it looks like you're building the URL in the browser somehow. You need to use the Javascript escape() function to handle URL-significant characters properly.

Ned Batchelder
`escape()` function do different thing in different browsers, so it would be better to use `encodeURIComponent()`.
Denis Otkidach
Actually escape is consistent over browsers... it's just consistently wrong. :-) encodeURIComponent is indeed the correct function for building query strings.
bobince