1

There is a from with various elements

<input name='id' value='123'/>
<input name='some_value' value='123'/>
<!-- thing that i want-->
<input name='array[key_1]' value='value_1'/>
<input name='array[key_2]' value='value_2'/>
<input name='array[key_3]' value='value_3'/>
<input name='array[key_4]' value='value_4'/>
</form>

If i've been used PHP, array data will be collected in $_POST["array"] So the questions is, how to collect dict "array" in django views?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Yuretz
  • 49
  • 8
  • 1
    Try printing `request.POST` – ilyasbbu Nov 04 '22 at 06:56
  • all objects are independent in that way – Yuretz Nov 04 '22 at 06:58
  • 1
    Does this answer your question? [retrieving list items from request.POST in django/python](https://stackoverflow.com/questions/5430470/retrieving-list-items-from-request-post-in-django-python) Note: You don't need `[key_1]` (and it won't work), etc. you have to name all of the inputs the same as `array` – Abdul Aziz Barkat Nov 04 '22 at 07:55
  • @AbdulAzizBarkat That's what I tried to tell, there's no need for accessing key value pairs for further operation. – Sunderam Dubey Nov 04 '22 at 09:46

3 Answers3

0

You can get all values of the form in the view by giving their name attribute in request.POST dict so:

def some_view(request):
    if request.method=='POST':
       id=request.POST.get('id')
       some_value=request.POST.get('some_value')
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • it that way i can't access to keys, only to values. in fact i need to get dict key=>value pairs of cource i can get all values and parse them manually, but i think that's not a good way, should be something more simple and beautilfull – Yuretz Nov 04 '22 at 07:10
  • @Yuretz But why do you want to access key value pairs, it's not necessary for saving the form or doing some further operation. Share your form and view and tell what you want to do exactly? – Sunderam Dubey Nov 04 '22 at 07:13
  • input form generates from database and when user press submit button i don't know exactly which keys will pass, that's why best way for me to get all variables with name "array" with key-value pair – Yuretz Nov 04 '22 at 07:25
  • @Yuretz Values passed will be in `request.POST` so `print(request.POST)` and you'll get all values with their name. – Sunderam Dubey Nov 04 '22 at 07:29
  • re-read my first reply ) – Yuretz Nov 04 '22 at 07:37
0

Try below code.

views.py:

def DemotimeView(request):
    if request.method == 'POST': 
        data = dict(request.POST)
        del data['csrfmiddlewaretoken']
        print({i:j[0] for i,j in data.items()})
    return render(request, 'index.html',)

Html:

<form action="" method="post">
     {% csrf_token %}
     <input name='id' value='123'/>
     <input name='some_value' value='123'/>
     <!-- thing that i want-->
     <input name='key_1' value='value_1'/>
     <input name='key_2' value='value_2'/>
     <input name='key_3' value='value_3'/>
     <input name='key_4' value='value_4'/>

     <button type="submit">Add</button>

</form>

Output:

{'id': '123', 'some_value': '123', 'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3', 'key_4': 'value_4'}
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
0

I'm not sure that it's the best way, but it's work. I've decide to rename params without [] and combine them with dict key with separator "__"

So, here it is

template:

<input type="text" name="name" 
        value='Yuretz'/>
<input type="text" name="last_name" 
        value='Oguretz'/>

{% for line in print_values %}
<div>
   <b>{{line.name}}</b><br/>
   <input type="text" 
      name="param__{{line.name}}"
      value="{{line.value}}"/>
</div>
{% endfor %}

views.py

params_data = {}    
all_post_data = request.POST.dict()
for line in all_post_data:
   if line.startswith('param__'):
       params_key = line.replace('param__','')
       params_data[params_key]=all_post_data[line]
### finally
print(params_data)

Hope that will help somebody

Yuretz
  • 49
  • 8