Warm tip: This article is reproduced from serverfault.com, please click

python-当你尝试通过POST方法保存在vars中时,Django不返回任何内容

(python - Django return none when y try to save in vars by POST method)

发布于 2020-12-01 01:25:40

我正在尝试通过POST方法从表单保存数据。当我尝试在views.py中保存在var中时,它没有放入

在这里,我显示了代码的重要部分:

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})

这是var的一张图片:

在此处输入图片说明

Questioner
Antonio Marfil
Viewed
0
Haseeb Ahmed 2020-12-01 10:16:27

你在评论中缺少上述内容,我认为你正在使用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,但是要发出请求,你需要name属性