/ addtocart / NOT NULL约束处的IntegrityError失败:stock_ordereditem.ordered_name

时间:2020-06-17 20:56:15

标签: django web django-models

当我尝试添加到购物车时,在/ addtocart /中收到错误消息'IntegrityError NOT NULL约束失败:stock_ordereditem.ordered_name' 这是我的代码

def addtocart(request):
    ordered_name = request.POST.get('ordered_name')
    price = request.POST.get('price')
    image = request.POST.get('image')

    put_item = OrderedItem(ordered_name=ordered_name, price=price, image=image)
    put_item.save()
    return redirect('cart')
class OrderedItem(models.Model):
    ordered_name = models.CharField(max_length=50)
    price = models.FloatField()
    image = models.ImageField(upload_to='pics')

    def __str__(self):
        return self.name
{% extends 'base.html' %}

{% block title %}
August.shop
{% endblock %}

{% block content %}

<h3>Ordered Items</h3>
<div class="container" style="margin-top: 50px;">
    <div class="row">
        {% for order in order %}
        <div class="col-xs-3">
            <div class="card" style="width: 18rem;">
                <img src="{{ order.image.url }}" class="card-img-top" alt="...">
                <div class="card-body">
                    <h5 class="card-title">{{ order.name }}</h5>
                    <p class="card-text">${{ order.price }}</p>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>

{% endblock %}

我感谢一个非常有用的答案。

0 个答案:

没有答案