0
在我的模型中,我創建了一個新的類別變體,其中我添加了3個變體,像我的第一個產品,如小,中,大。但我想要的是根據變化傳遞價格,並且如果某些產品沒有任何變化,那麼它必須通過數據庫中先前保存的基本價格。打印對應於不同尺寸的商品的價格,如果尺寸沒有變化,則打印底價
車應用
views.py
from django.shortcuts import render, HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your views here.
from products.models import Product
from .models import Cart, CartItem
def view(request):
try:
the_id = request.session['cart_id']
except:
the_id = None
if the_id:
cart = Cart.objects.get(id=the_id)
context = {"cart": cart}
else:
empty_message = "Your Cart is Empty, please keep shopping."
context = {"empty": True, "empty_message": empty_message}
template = "cart/view.html"
return render(request, template, context)
def update_cart(request, id):
request.session.set_expiry(120000)
try:
qty = request.GET.get('qty')
update_qty = True
except:
qty = None
update_qty = False
try:
the_id = request.session['cart_id']
except:
new_cart = Cart()
new_cart.save()
request.session['cart_id'] = new_cart.id
the_id = new_cart.id
cart = Cart.objects.get(id=the_id)
try:
product = Product.objects.get(id=id)
except Product.DoesNotExist:
pass
except:
pass
cart_item, created = CartItem.objects.get_or_create(cart=cart, product=product)
if created:
print "yeah"
if update_qty and qty:
if int(qty) == 0:
cart_item.delete()
else:
cart_item.quantity = qty
cart_item.save()
else:
pass
new_total = 0.00
for item in cart.cartitem_set.all():
line_total = float(item.product.price) * item.quantity
new_total += line_total
request.session['items_total'] = cart.cartitem_set.count()
cart.total = new_total
cart.save()
return HttpResponseRedirect(reverse("cart"))
產品應用
models.py
from django.db import models
# Create your models here.
class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField(null=True, blank=True)
price = models.DecimalField(decimal_places=2, max_digits=100, default=29.99)
sale_price = models.DecimalField(decimal_places=2, max_digits=100, null=True, blank=True)
#image = models.FileField(upload_to='products/images/', null=True)
slug = models.SlugField()
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
update = models.DateTimeField(auto_now_add=False, auto_now=True)
active = models.BooleanField(default=True)
def __unicode__(self):
return self.title
def get_price(self):
return self.price
class ProductImage(models.Model):
Product = models.ForeignKey(Product)
image = models.ImageField(upload_to='products/images/')
featured = models.BooleanField(default=False)
thumbnail = models.BooleanField(default=False)
active = models.BooleanField(default=True)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self):
return self.Product.title
class Variation(models.Model):
product = models.ForeignKey(Product)
#category = models.CharField(max_length=120, choices=VAR_CATEGORIES, default='size')
title = models.CharField(max_length=120)
image = models.ForeignKey(ProductImage, null=True, blank=True)
price = models.DecimalField(max_digits=100, decimal_places=2, null=True, blank=True)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
active = models.BooleanField(default=True)
#objects = VariationManager()
def __unicode__(self):
return self.title
HTML塊
{% block product%}
<div class='row'>
{% for product in products %}
<div class='col-sm-4'>
{% for item in product.productimage_set.all %}
<div class="thumbnail">
<img class='img-responsive' src="{{ MEDIA_URL }}{{ item.image }}" /><br>
<div class="caption">
<h3>{{ product.title }}</h3>
<h5>{{ product.price }}</h5>
<p>{{ product.description }}</p>
<form method='GET' action='{% url "update_cart" product.id %}'>
<input name='qty' type='number' value='1' />
<input type='submit' class="btn btn-primary" value='Add to cart'/>
{% if product.variation_set.all %}
<select name='size'>
{%for item in product.variation_set.all %}
<option value='{{ item.title|lower }}'>{{ item.title|capfirst }}</option>
{% endfor %}
</select>
{% endif %}
</form>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
{% endblock %}
{% block content %}
{% endblock %}
在這裏,我沒有得到兩件事情之一是返回preferred_price和通話即是Purchase.get_price能否請你解釋一下被稱爲進一步 –
這是僞代碼展示過程。您必須在此函數中編寫邏輯本身以決定從哪個相關變體中檢索價格。我不知道你想如何工作。 –
上面的僞代碼的問題是它會改變價格,但不是根據大小 –