django商店-创建订单

问题描述 投票:0回答:1

我创建了一个网站,用户可以在不付款的情况下下订单。假设用户选择了产品,然后在按下“订购”按钮后选择了购物车。他必须提供该订单的数据。

我的表单未保存在订单表中。我没有完整的想法。有人可以看到我所缺少的吗?

从此刻开始if request.method == 'POST':直到此时返回

redirect('cart:cart_detail')
      except ObjectDoesNotExist:
          pass 

该代码不起作用。

cart / views.py

def cart_detail(request, total=0, counter=0, cart_items = None):
  try:
      cart = Cart.objects.get(cart_id=_cart_id(request))
      cart_items = CartItem.objects.filter(cart=cart, active=True)
      for cart_item in cart_items:
          total += (cart_item.product.price * cart_item.quantity)
          counter += cart_item.quantity
  except ObjectDoesNotExist:
      pass
  try:  
    if request.method == 'POST':
      order_details = Order.objects.create(
            token = token,
            total = total,
            emailAddress = email,
            billingName = billingName,
            billingAddress1 = billingAddress1,
            billingCity = billingcity,
            billingPostcode = billingPostcode,
            billingCountry = billingCountry,
            shippingName = shippingName,
            shippingAddress1 = shippingAddress1,
            shippingCity = shippingcity,
            shippingPostcode = shippingPostcode,
            shippingCountry = shippingCountry
        )
      order_details.save()
      for order_item in cart_items:
                oi = OrderItem.objects.create(
                        product = order_item.product.name,
                        quantity = order_item.quantity,
                        price = order_item.product.price,
                        order = order_details
                    )
                oi.save()
                '''Reduce stock when order is placed or saved'''
                products = Product.objects.get(id=order_item.product.id)
                products.stock = int(order_item.product.stock - order_item.quantity)
                products.save()
                order_item.delete()
                '''The terminal will print this message when the order is saved'''
                print('The order has been created')
      return redirect('cart:cart_detail')
  except ObjectDoesNotExist:
      pass
  return render(request, 'cart/cart.html', dict(cart_items = cart_items, total = total, counter = counter))

templates / shop / cart.html

    <button class="btn send-click btn-md my-1 btn-block" data-toggle="modal" data-target="#inquiryModal">Complete Order</button>

                                <!-- Inquiry Modal -->
<div class="modal fade" id="inquiryModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header">
        <h5 class="modal-title" id="inquiryModalLabel">Complete Order</h5>
        <button type="button" class="close" data-dismiss="modal">
            <span>&times;</span>
        </button>
    </div>
    <div class="modal-body">
            <form action="{% url 'cart_detail' %}" method="POST">
          {% csrf_token %}

            <div class="form-group">
                <label for="emailAddress" class="col-form-label">Email:</label>
                <input type="text" name="emailAddress" class="form-control" 
                value="" required>
            </div>

            <div class="form-group">
                    <label for="billingName" class="col-form-label">Name:</label>
                    <input type="text" name="billingName" class="form-control" 
                    value="" required>
                </div>

                <div class="form-group">
                        <label for="billingAddress1" class="col-form-label">Adress:</label>
                        <input type="text" name="billingAddress1" class="form-control" 
                        value="" required>
                    </div>

                    <div class="form-group">
                            <label for="billingCity" class="col-form-label">City:</label>
                            <input type="text" name="billingCity" class="form-control" 
                            value="" required>
                        </div>

                        <div class="form-group">
                                <label for="billingPostcode" class="col-form-label">Post code:</label>
                                <input type="text" name="billingPostcode" class="form-control" 
                                value="" required>
                        </div>

                        <div class="form-group">
                                <label for="billingCountry" class="col-form-label">Country:</label>
                                <input type="text" name="billingCountry" class="form-control" 
                                value="" required>
                            </div>

                            <div class="form-group">
                                    <label for="shippingName" class="col-form-label">Shop name:</label>
                                    <input type="text" name="shippingName" class="form-control" 
                                    value="" required>
                                </div>

                                <div class="form-group">
                                        <label for="shippingAddress1" class="col-form-label">Shop adress:</label>
                                        <input type="text" name="shippingAddress1" class="form-control" 
                                        value="" required>
                                    </div>

                                    <div class="form-group">
                                            <label for="shippingCity" class="col-form-label">Shop City:</label>
                                            <input type="text" name="shippingCity" class="form-control" 
                                            value="" required>
                                        </div>

                                        <div class="form-group">
                                                <label for="shippingPostcode" class="col-form-label">Shop Post code:</label>
                                                <input type="text" name="shippingPostcode" class="form-control" 
                                                value="" required>
                                            </div>

                                            <div class="form-group">
                                                    <label for="shippingCountry" class="col-form-label">Shop Country:</label>
                                                    <input type="text" name="shippingCountry" class="form-control" 
                                                    value="" required>
                                                </div>
                                            <hr>
                                <input type="submit" value="Order" class="btn send-click btn-md my-0 btn-block">
                        </form>
                    </div>
                </div>

</div>
</div>

orders / models.py

from django.db import models

class Order(models.Model):
    token = models.CharField(max_length=250, blank=True)
    total = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='GBP Order Total')
    emailAddress = models.EmailField(max_length=250, blank=True, verbose_name='Email Address')
    created = models.DateTimeField(auto_now_add=True)
    billingName = models.CharField(max_length=250, blank=True)
    billingAddress1 = models.CharField(max_length=250, blank=True)
    billingCity = models.CharField(max_length=250, blank=True)
    billingPostcode = models.CharField(max_length=10, blank=True)
    billingCountry = models.CharField(max_length=200, blank=True)
    shippingName = models.CharField(max_length=250, blank=True)
    shippingAddress1 = models.CharField(max_length=250, blank=True)
    shippingCity = models.CharField(max_length=250, blank=True)
    shippingPostcode = models.CharField(max_length=10, blank=True)
    shippingCountry = models.CharField(max_length=200, blank=True)

    class Meta:
        db_table = 'Order'
        ordering = ['-created']

    def __str__(self):
        return str(self.id)


class OrderItem(models.Model):
    product = models.CharField(max_length=250)
    quantity = models.IntegerField()
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='GBP Price')
    order = models.ForeignKey(Order, on_delete=models.CASCADE)

    class Meta:
        db_table = 'OrderItem'

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

    def __str__(self):
        return self.product
python django django-models django-templates django-views
1个回答
0
投票

我更新了您的购物车/views.py

def cart_detail(request, total=0, counter=0, cart_items = None):
if request.method == 'POST':
    order_details = Order.objects.create(token = token,
                                         total = total,
                                         emailAddress = email,
                                         billingName = billingName,
                                         billingAddress1 = billingAddress1,
                                         billingCity = billingcity,
                                         billingPostcode = billingPostcode,
                                         billingCountry = billingCountry,
                                         shippingName = shippingName,
                                         shippingAddress1 = shippingAddress1,
                                         shippingCity = shippingcity,
                                         shippingPostcode = shippingPostcode,
                                         shippingCountry = shippingCountry)

    # If you use object.create method you don't have to use .save()
    order_details.save()
    for order_item in cart_items:
        oi = OrderItem.objects.create(product = order_item.product.name,
                                      quantity = order_item.quantity,
                                      price = order_item.product.price,
                                      order = order_details)
        # Same here
        oi.save()
        '''Reduce stock when order is placed or saved'''
        products = Product.objects.get(id=order_item.product.id)
        products.stock = int(order_item.product.stock - order_item.quantity)
        products.save()
        order_item.delete()
        '''The terminal will print this message when the order is saved'''
        print('The order has been created')
    return redirect('cart:cart_detail')
else:
    try:
        cart = Cart.objects.get(cart_id=_cart_id(request))
        cart_items = CartItem.objects.filter(cart=cart, active=True)
        for cart_item in cart_items:
            total += (cart_item.product.price * cart_item.quantity)
            counter += cart_item.quantity

        return render(request, 'cart.html', dict(cart_items = cart_items, total = total, counter = counter))
    except Cart.DoesNotExist:
        pass

如果显示任何错误让我知道,请尝试此>]

© www.soinside.com 2019 - 2024. All rights reserved.