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?

Jeremy W
  • 1,889
  • 6
  • 29
  • 37
Modelesq
  • 5,192
  • 20
  • 61
  • 88

2 Answers2

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.

user2390182
  • 72,016
  • 6
  • 67
  • 89
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()
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • 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. – Modelesq May 26 '16 at 15:10
  • 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. – Shang Wang May 26 '16 at 15:11
  • 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? – Modelesq May 26 '16 at 15:15
  • 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).https://docs.python.org/2/library/json.html – Shang Wang May 26 '16 at 15:19
  • 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: https://docs.djangoproject.com/en/1.9/ref/request-response/#jsonresponse-objects – Shang Wang May 26 '16 at 15:20
  • Any idea on how to avoid hidden inputs being placed within dict? – Modelesq May 26 '16 at 15:47
  • Just get the whole dict and manually get rid of the entries that are hidden: http://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary – Shang Wang May 26 '16 at 15:51