2

I have a form that holds multiple inputs... Within the request.POST, I'm looping through all of the input values. But I'd like to store them within a variable.. How do I do that?

for key, value in request.POST.items(): print(key, value) # how can I store instead of print? 

How can I store all values in a python array/dict/whatever?

6
  • 1
    You mentioned you had a form, so I would imagine it's a lot easier to loop on the form field instead. Are you aware of that? Commented May 26, 2016 at 14:50
  • @ShangWang No. Love to try it. How can I loop through form fields and store the data? Commented May 26, 2016 at 15:00
  • Why you don't do this mydict = request.POST? or mydict = request.POST.copy() Commented May 26, 2016 at 15:01
  • what is .copy() ?@trinchet Commented May 26, 2016 at 15:14
  • stackoverflow.com/questions/12611345/… Commented May 26, 2016 at 15:48

2 Answers 2

1

You have couple of ways to store the POST data in a local variable. The question is: why would you want to do that when you have access to request.POST anyway?

# Note: all keys and values in these structures are strings # easiest, but immutable QueryDict d = request.POST # dict d = dict(request.POST.items()) # array a = list(request.POST.items()) # list of key-value tuples a = request.POST.values() # list of values only 

These variables will only live for the current request-response cycle. If you want to persist any of the data beyond that, you would have to store them to the database. Moreover, I recommend using a django form to handle POST data. That will take care of validation, type-casting, etc. for you.

Sign up to request clarification or add additional context in comments.

5 Comments

Awesome. To answer your question, I'm using request.POST to get some data to send to an api.
I come from a javascript background, is there such thing as objects? Like could I store all the values in an object?
Does .values() include hidden inputs? (I'd like to avoid hidden inputs if possible)
Everything in Python is an object. A dict is most easily converted to JSON string via json.dumps(d). .values() does include hidden inputs!
How can I avoid hidden inputs?
0

This might not directly answer your question, but I don't recommend accessing request.POST directly since you already had a form. Form is good that it abstracts away a lot of the raw data that you need to handle by encapsulating them in the form object, so I would suggest check the form itself for data:

form = YourForm(request.POST or None) if form.is_valid(): field1_value = form.cleaned_data['field1'] field2_value = form.cleaned_data['field2'] 

django doc has examples about how to access form fields like I did.

Also, if you want to get a copy of mutable dict object same as request.POST, you could do:

post_copy = request.POST.copy() 

7 Comments

The reason why I have a form is because I'm grabbing data from the form and sending to an API. it was the best way I could think of.
That's totally a good way of doing it, I'm just suggesting using form to fetch data instead of accessing raw data from request.POST which could be very painful.
So i come from a js background, is a dict an object? Like if I sent the json string, would it appear like a js object?
In python dict is an object, everything is an object in python. If you have a json string in python, you need to do json.loads(json_string) to convert json string to python dict(like a hash in js).docs.python.org/2/library/json.html
If you want to send a python dict to the template and be understood by js, it's the other way round: json.dumps(dict), or just use a shortcut JSONResponse in django: docs.djangoproject.com/en/1.9/ref/request-response/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.