我已經包含隱藏輸入標籤,當我發佈形式的意見和打印表格,我看到我需要的值,但內容沒有保存到數據庫中 這裏是我的HTML保存HTML輸入標籤Django模型
<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>
這裏是我的views.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})
這裏是model.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
有使其保存到數據庫中,我創建名爲車的方式
只需調用save方法:'items.save()' – Selcuk
@Selcuk獲取[u''價格'值必須是十進制數。「]作爲錯誤 – uche
您輸入了什麼價格值? – v1k45