Posts Tagged ‘immutable’

Making a Django POST mutable

July 9, 2010

For good reasons, the Django Request object’s POST attribute is immutable. No one wants anyone mucking around with important data there. But I had a case where I wanted to re-use some view code and it would involve using a POST object again, but with a few added parameters.

You can copy the POST data into a QueryDict, but I found that if you want to forward the actual request, you have to copy.copy() the request using the python standard copy library. Maybe this is obvious but it might save someone(like me) an hour of searching and trial/error.

def my_view(request)
    req_copy = copy.copy(request)
    req_copy.POST = request.POST.copy()
    req_copy.POST.update({'added_param':'my_value'})
    my_DRY_view(req_copy)

def my_DRY_view(req_copy)
        cool_stuff()

Follow

Get every new post delivered to your Inbox.