如何在Django中呈现Buy now视图?

2024-05-14 08:24:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在建立一个电子商务网站。我有结帐功能,这将允许用户购买产品。我已经为购物车做到了这一点,即当用户将产品添加到购物车中时,他才能够购买产品。但是我想添加一个功能,允许用户从product_detail页面购买单个产品,这样,在不将产品添加到购物车的情况下,用户就可以购买。所以问题是,我想在我的结账页面上做单一产品的事情。但也有来自购物车的产品。如何允许用户购买产品而不将其添加到购物车中

这是views.py文件:

def checkout(request):
    user = request.user
    address = Customer.objects.filter(user=user)
    cart_items = Cart.objects.filter(user=user)
    amount = 0
    shipping_amount = 70
    cart_product = [p for p in Cart.objects.all() if p.user == user]

    if cart_product:
        for p in cart_product:
            temp_amount = (p.quantity * p.product.discounted_price)
            amount += int(temp_amount)
            total_amount = amount + shipping_amount

    return render(request, 'app/checkout.html', {'address':address,
      'cart_items':cart_items, 'total_amount':total_amount,
      'amount':amount})

这是结帐模板:

{% extends 'app/base.html' %}
{% load static %}
{% block title %}Checkout{% endblock title %}
{% block main-content %}z
{% load humanize %}

<div class="container">
 <div class="row mt-5">
  <div class="col-sm-6">
    <h4>Order Summary</h4>
    <hr>
    {% for cart in cart_items %}
      <div class="card mb-2">
        <div class="card-body">
          <p class="font_weight">Product: {{cart.product.title}}</p>
          <p>Quantity: {{cart.quantity}}</p>
          <p class="font_weight">Price: {{cart.total_cost|intcomma}}</p>
        </div>
      </div>
     {% endfor %}
    <p class='mx-2 fw-bold'>Total Cost + Rs.70 = {{total_amount|intcomma}}.</p>
    <small><span class='fw-bold'>Terms and Conditions:</span> Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia, ullam saepe! Iure optio repellat dolor velit, minus rem. Facilis cumque neque numquam laboriosam, accusantium adipisci nisi nihil in et quis?</small>
  </div>
  <div class="col-sm-4 offset-sm-1">
    <h4>Select Shipping Address :</h4>
    <hr>
    <form action="/payment_done/">
    {% for add in address %}
      <div class="card">
        <div class="card-body">
        <h5>{{add.name}}</h5>
        <p>{{add.locality}}, {{add.city}}, {{add.state}} - {{add.zipcode}}.</p>
        </div>
      </div>
        <div class="form-check mt-2 mb-5">
          <input class="form-check-input" type="radio" name="custid" id="custadd{{forloop.counter}}" value="{{add.id}}" required>
          <label class="form-check-label fw-bold" for="custadd{{forloop.counter}}">
          Address: {{forloop.counter}} </label>
         </div>
    {% endfor %}
        <div class="text-end">
          <button type="submit" class="btn btn-warning mt-3 px-5 fw-bold">Continue</button>
        </div>
      </form>
    </div>
  </div>
</div>
{% endblock main-content %}

这是模型。py:

class Product(models.Model):
    title = models.CharField(max_length = 100)
    selling_price = models.DecimalField(max_digits=15, decimal_places=2)
    discounted_price = models.DecimalField(max_digits=15, decimal_places=2)
    description = models.TextField()
    brand = models.CharField(max_length = 100)
    category = models.CharField(choices = CATEGORY_CHOICES,  max_length = 3)
    product_image = models.ImageField(upload_to='productimg')

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

    @property
    def save_amount(self):
        return self.selling_price - self.discounted_price

class Cart(models.Model):
    user = models.ForeignKey(User, on_delete= models.CASCADE)
    product = models.ForeignKey(Product, on_delete= models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)

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

    @property
    def total_cost(self):
        return self.quantity * self.product.discounted_price

Tags: 用户selfdivadd产品modelsdefproduct

热门问题