I want to receive a request.POST values N times, where N is a number entered by the user. The views.py is:
def valores(request):
global peso_unitario, preco_unitario
peso_unitario = []
preco_unitario = []
N = a
print('N=' + str(N))
for i in range(N):
form = Form(request.POST)
c = int(request.POST.get('peso_u'))
d = int(request.POST.get('preco_u'))
peso_unitario.append(c)
preco_unitario.append(d)
return render(request, 'valores.html')
return render(request, 'pacote_m.html', {'peso_unitario': peso_unitario, 'preco_unitario': preco_unitario})
In this code in the end I have two returns, where the first is inside the for loop because I want to receive the values N times and return them to the template valores.html
. The last return is in the end, after the for loop will redirect to a new template, this template pacote.html
will show the values calculated according to I programmed, but it isn’t the problem.
The template valores.html
is:
{% extends 'basic.html' %}
{% block content %}
<form action="page2" method="GET">
{% csrf_token %}
<h1>Digitados:</h1>
numero de objetos={{n_objetos}}<br>
peso peso maximo={{peso_maximo}}<br>
<!--- Peso unitario: <input type="text" name="peso_u"><br><br>
Preco unitario: <input type="text" name="preco_u"><br><br> --->
<table>
<thead>
<tr>
<th>Peso unitario</th>
<th>Preco unitario</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="peso_u"></td>
{{form.peso_u}}
<td><input type="text" name="preco_u"></td>
{{form.preco_u}}
</tr>
</table>
<input type="submit">
</form>
{% endblock %}
My forms.py is:
class Form(forms.Form):
peso_u = forms.CharField(max_length=50)
preco_u = forms.CharField(max_length=50)
The problem is I can’t get the N
request.POST values. I realized that it gets only once the request.POST values, the following requests substitute the values entered before and it will continue in the loop, I realized that the loop is forever because it will request more than N
times the values, maybe because it substitute the value entered before.