python - 将 html 输入标签保存到 django 模型

标签 python html django

我有一个包含隐藏输入标签的表单,当我将表单发布到 View 并打印时,我看到了我需要的值,但内容没有保存到数据库中 这是我的 html

<form method="POST" action="/selly/cart/" item_id="{{product.pk}}" enctype="multipart/form-data">
    {% csrf_token %}
    <h1 name="description">Description is : {{each_item.description}}</h1>
    <p><input type="hidden" name="description" value="{{each_item.description}}"></p>

    <span name="price">Price is : $ {{each_item.price}}/piece</span>
    <p><input type="hidden" name="price" value ="{{each_item.price}}"></p>

    <p>Quantity is : <input type="number" default="0" name="quantity"> piece ( {{each_item.item_remaining}} pieces available )</p>
    <br>
    <input type="submit" class="btn btn-primary" value="Add to Cart">

</form>

这是我的观点.py

from selly.models import Cart
def cart(request):
    if request.method == "POST":
        print "rp ", request.POST

        description = request.POST['description']
        print "Description is ", description

        price = request.POST['price']
        print "Price is ", price

        quantity = request.POST['quantity']
        print "Quantity is ", quantity

        items =  Cart.objects.get_or_create(client="client", description="description", price="price", quantity="quantity")
        print "ITEMS", items
    return render(request, 'selly/cart.html', {'items': items})

这是模型.py

class Cart(models.Model):
    description = models.CharField(max_length = 100)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.IntegerField()

    def __str__(self):
        return self.description

    def total(self):
        return self.price * self.quantity

有没有办法让它保存到我创建的名为 Cart 的数据库

最佳答案

我很惊讶您没有收到该代码的错误 - get_or_create 根据文档返回一个元组:

Returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.

所以你的行items = Cart.objects.get_or_create(client="client",description="description",price="price",quantity="quantity")

需要

元素,已创建 == Cart.objects.get_or_create(client="client", description="description", Price="price", amount="quantity")

您还可以询问创建的变量,因为如果它为 false,则它尚未创建新对象;如果它没有创建新对象,那么 get_or_create 所做的就是返回数据库中已有的对象。

如果要更新对象,则需要手动保存该对象。

这是因为:

This is meant as a shortcut to boilerplatish code. For example:

try:
    obj = Person.objects.get(first_name='John', last_name='Lennon') 
except Person.DoesNotExist:
    obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
    obj.save()

完整详细信息位于文档页面 for get_or_create.

此外,您的最后一行是错误的 - 您正在将字符串分配给对象,您在其中执行 price="price" - 您实际上想要执行 price=price >,因为您要分配 object.price=price,这就是您在上面代码中所说的变量。我建议可能将这些变量称为“incoming_price”或类似变量以避免阴影/困惑。

关于python - 将 html 输入标签保存到 django 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36307948/

相关文章:

python - 更改 ttk 条目焦点颜色 - Python

javascript - 使用 angularjs 在 div 标签之间插入 html

python - Django 上的正则表达式不起作用

django - 在 Django 中使用 ".filter().filter().filter()..."有什么缺点吗?

javascript - 如何删除多维数组中的图层

python - Django 中的下拉搜索字段

java - 进口声明的历史是什么?

python - Python中的_winreg.CreateKey问题

python - Django 呈现模板 `500.html` 而不是 `404.html`

jquery - 如何模仿 jquery 中的 marque 滚动行为?