Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why copy post data in Django instead of working with it directly?

Tags:

python

django

Django code samples involving post data often shows code similar to this:

if request.method == "POST":
   post = request.POST.copy()
   #do stuff with post data

Is there a reason for copying the post data instead of working with it directly?

like image 453
Jeff Avatar asked Feb 26 '10 06:02

Jeff


People also ask

How does Django read POST data?

Django pass POST data to view The input fields defined inside the form pass the data to the redirected URL. You can define this URL in the urls.py file. In this URLs file, you can define the function created inside the view and access the POST data as explained in the above method. You just have to use the HTTPRequest.

What does POST mean in Django?

The result of request. method == "POST" is a boolean value - True if the current request from a user was performed using the HTTP "POST" method, of False otherwise (usually that means HTTP "GET", but there are also other methods).

How do you receive data from a Django form with a POST request?

Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.

What does request POST do in Django?

Django puts data in request. POST when a user submits a form with the attribute method="post" . Line 10: You retrieve the submitted value by accessing the data at the key "follow" , which you defined in your template with the name HTML attribute on your <button> elements.


1 Answers

I think it is because request.POST itself is defined immutable. If you want a version you can actually change (mutability), you need a copy of the data to work with.

See this link (request.POST is a QueryDict instance).


class QueryDict

QueryDict instances are immutable, unless you create a copy() of them. That means you can’t change attributes of request.POST and request.GET directly.

like image 146
ChristopheD Avatar answered Sep 24 '22 22:09

ChristopheD