当y尝试通过POST方法在VAR中保存时,Django返回none

2024-04-27 03:45:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过POST方法保存表单中的数据。当我尝试在views.py的vars中保存时,它将放入none中

这里我展示了代码的重要部分:

add.html:

<form method="POST">
      {% csrf_token %}
      <h5>AÑADIR PEDIDO</h5>
      <div class="form-group">
        <label for="cliente">Nombre del Cliente</label>
        <input type="text" class="form-control" id="cliente" placeholder="pepito" />
      </div>
      <div class="form-group">
        <label for="producto">Codigo del Producto</label>
        <select class="form-control" id="producto">
          {% for cantidades in cantidad_pedido %}
          <option value="{{cantidades.Cproducto}}">
            {{cantidades.Cproducto}}
          </option>
          {% endfor %}
        </select>
      </div>
      <div class="form-group">
        <label for="cantidad">Cantidad de dicho producto</label>
        <input type="number" class="form-control" id="cantidad" placeholder="cantidad" />
      </div>
      <button type="submit" class="btn btn-dark" style="margin-bottom: 1%;">Añadir!</button>
    </form>

models.py:

class Stock(models.Model):
Cproducto = models.CharField(max_length=50, primary_key=True)
cantidad = models.IntegerField()

class Pedido(models.Model):
Cpedido = models.CharField(max_length=50, primary_key=True)
Ccliente = models.CharField(max_length=50)
FechaPedido = models.DateField(default=datetime.now)

class detallePedido(models.Model):
Cpedido = models.ForeignKey(Pedido, on_delete=models.CASCADE)
Cproducto = models.ForeignKey(Stock, on_delete=models.CASCADE)
Cantidad = models.IntegerField()

views.py:

def add(request):
cantidad_pedido = Stock.objects.all()
if request.POST:
    client = request.POST.get('cliente')
    producto = request.POST.get('producto')
    cantidad = request.POST.get('cantidad')
    stock_producto = Stock.objects.get(Cproducto=producto)

    if(cantidad > stock_producto.cantidad):
        messages.error(request, 'Error, la cantidad es mayor')

return render(request, 'add.html', {'cantidad_pedido': cantidad_pedido})

这里有一张vars的照片:

enter image description here


Tags: pydivformforgetmodelsrequeststock
1条回答
网友
1楼 · 发布于 2024-04-27 03:45:58

正如上面评论中提到的,您失踪了 我认为您正在使用id属性发出请求,但是您需要像这样使用name属性来代替id

form method="POST">
      {% csrf_token %}
      <h5>AÑADIR PEDIDO</h5>
      <div class="form-group">
        <label for="cliente">Nombre del Cliente</label>
        <input type="text" class="form-control" name="cliente" placeholder="pepito" />
      </div>
      <div class="form-group">
        <label for="producto">Codigo del Producto</label>
        <select class="form-control" name="producto">
          {% for cantidades in cantidad_pedido %}
          <option value="{{cantidades.Cproducto}}">
            {{cantidades.Cproducto}}
          </option>
          {% endfor %}
        </select>
      </div>
      <div class="form-group">
        <label for="cantidad">Cantidad de dicho producto</label>
        <input type="number" class="form-control" name="cantidad" placeholder="cantidad" />
      </div>
      <button type="submit" class="btn btn-dark" style="margin-bottom: 1%;">Añadir!</button>
    </form>

您可以同时使用id和name,但为了发出请求,您需要命名属性

相关问题 更多 >