Input HTML and Django Request.POST array

Asked

Viewed 123 times

1

I’m trying to do a POST with Formdata for a view and I’m not getting the input array. Follow the example:

HTML

<input name="bonus" type="number" placeholder="ex.: 100.000,00">
{% for x in 'xxxxx' %}
    <input name="pontos[]" type="number" placeholder="ex.: 100.000,00">
    <input name="nomes[]" type="text" price placeholder="ex.: 1.000,00">
{% endfor %}

py views.

bonus = request.POST.get('bonus', None)
pontos = request.POST.getlist('pontos[]', None)
nomes = request.POST.getlist('nomes[]', None)
print(bonus, pontos, nomes)

output

>>> 10 [None] [None]

How much do I print on request.POST appears this:

<QueryDict: { 'bonus': 10, 'pontos[]': [None], 'nomes[]': [None]}>

Seeing on the Chrome Network, is going all fields straight.

bonus: 10.00
pontos[]: 10000
pontos[]: 100100110
pontos[]: 
pontos[]: 
pontos[]: 
nomes[]: Guilherme
nomes[]: Joao
nomes[]: 
nomes[]: 
nomes[]: 
  • In the back vc is rescuing as array, but is sending only the last field filled, because all names will be equal in the loop. Try changing the field name to pontos[] and nomes[] respectively, thus making them an array.

  • @Giovancruz made the modification (had already tested before) and the print is 10 [None] [None]... Strange that the other field picks up, now that array does not.. will have something to do with using Formdata?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.