Alien Life Form
Alien Life Form

Reputation: 1944

Django: Convert a POST request parameters to query string

As per subject, I am receiving a POST request. I want to convert it to the equivalent GET request and pass it to a template (so I can use it as href target in click-to-sort column headers).

Is there a pre-baked way to do it, or do I need to roll my own?

Cheers, alf

UPDATE

This is what I eventually used. I needed request.REQUEST, so request.POST.urlencode was not cutting it, and request.REQUEST does not have urlencode.

import urllib
def buildqs(request):
    "Builds a GET-style query string form http-request"
     #also request.POST.urlencode is possible - 
     #Sadly, request.REQUEST does not have urlencode
     return  urllib.urlencode(request.REQUEST)+"&"

Upvotes: 6

Views: 6970

Answers (3)

Jj.
Jj.

Reputation: 3160

From the docs

request.META['QUERY_STRING']

Upvotes: 1

mossplix
mossplix

Reputation: 3865

Use

request.POST.urlencode()

Upvotes: 11

bradley.ayers
bradley.ayers

Reputation: 38382

Use the standard library, urllib.urlencode. e.g.

>>> import urllib
>>> urllib.urlencode({'a': 1, 'b': 2})
'a=1&b=2'

Upvotes: 7

Related Questions