I am working with Django
, and I want to insert some values in the database, but they are not inserted and do not emit any errors.
From a selected product, I try to show the user a form that shows the selected product, allows you to select in a select
the ueb and specify the quantity you want to distribute of that previously selected product to the specified ueb. (Understand how ueb, can be an entity).
This is my code:
models:
class Distrib_Productos(models.Model):
nombre_UEB = models.ForeignKey(UEB)
productos = models.ForeignKey(Producto)
cantidad_a_asignar = models.IntegerField()
def __unicode__(self):
return self.nombre_UEB
form:
class Distrib_ProductosForm(ModelForm):
class Meta:
model = Distrib_Productos
views
def realizar_distribucion(request, id):
producto = Producto.objects.get(id=id)
uebs = UEB.objects.all()
return render_to_response('administrador/inserciones/distribucion_productos.html',
{'action': 'realizardistribucion/' + id, 'producto': producto, 'uebs': uebs},
context_instance=RequestContext(request)
)
def realizardistribucion(request, id):
producto = Producto.objects.get(id=id)
if request.method == 'POST':
cantidad = request.POST['cantidad']
uebs = request.POST.getlist('nombre_UEB')
for ueb in uebs:
distribucion = Distrib_Productos.objects.get_or_create(nombre_UEB=ueb,
productos=producto,
cantidad_a_asignar = cantidad)
return realizar_distribucion(request,id)
html
<div id="style">
<form method="post" id="style_admin2" action="/{{ action }}/">{% csrf_token %}
<table>
<tbody>
<tr>
<td><label id="prod">Producto</label></td>
<td>
<input type="text" name="productos" value="{{ form.productos}}"
class="form-control" id="contactos3"
placeholder="{{ producto.nombre }}">
</td>
</tr>
<tr>
<td colspan="2"><p class="help-block" > * Debes seleccionar la UEB a la que le asignarás el producto</p></td>
</tr>
<tr>
<td><label id="prod">Nombre de UEB</label></td>
<td>
<select>
{% for ueb in uebs %}
<option name="nombre_UEB" value="{{ ueb.id}}">{{ ueb.nombre }}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<td><label id="prod">Disponibilidad</label></td>
<td>
<table class="table table-bordered table-striped table-hover" id="texto_distrib">
<thead>
<th id="detalles_distrib">Cantidad en Almacén</th>
<th id="detalles_distrib">Tipo de Movimiento</th>
<th id="detalles_distrib">Precio CUP</th>
<th id="detalles_distrib">Precio CUC</th>
</thead>
<tbody>
<tr>
<td id="detalles_distrib">{{ producto.cantidad }}</td>
<td id="detalles_distrib">{{ producto.tipo_movimiento }}</td>
<td id="detalles_distrib">{{ producto.precio_CUP }}</td>
<td id="detalles_distrib">{{ producto.precio_CUC }}</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td><label id="prod">Cantidad a Asignar</label></td>
<td><input type="number" name="cantidad" class="form-control" id="contactos3"
placeholder="cantidad a asignar"></td>
</tr>
</tbody>
</table>
<button id="buscar" type="submit" class="btn btn-primary"><strong>Aceptar</strong></button>
</form>
</div>