我想用Python-Django创建一个网站,就我而言,我想将购物车中的商品添加到订单信息中。 订单模型包含用户信息,而OrderItem模型包含Item引用订单信息。
然后我要清除购物车
model.py
class Order(models.Model):
full_name = models.CharField(max_length=50)
address = models.CharField(max_length=50)
city = models.CharField(max_length=15)
state = models.CharField(max_length=15)
postcode = models.CharField(max_length=10)
phone = models.CharField(max_length=15)
email = models.EmailField()
class OrderItem(models.Model):
order = models.ForeignKey(Order, related_name='item', on_delete=models.CASCADE)
product = models.ForeignKey(Product, related_name='order_items', on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=0)
quantity = models.PositiveIntegerField(default=1)
admin.py
from .models import Member, Product, Image, Category, Order, OrderItem
class OrderItemInline(admin.TabularInline):
model = OrderItem
raw_id_fields = ['product']
class OrderAdmin(admin.ModelAdmin):
list_display = ['id', 'full_name', 'address', 'city', 'state', 'postcode', 'phone', 'email']
inlines = [OrderItemInline]
admin.site.register(Order, OrderAdmin)
form.py
class OrderCreateForm(forms.Form):
full_name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
address = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
city = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
state = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
postcode = forms.CharField(widget=forms.NumberInput(attrs={'class':'form-control'}), required=True)
phone = forms.CharField(widget=forms.NumberInput(attrs={'class':'form-control'}), required=True)
email = forms.EmailField(widget=forms.EmailInput(attrs={'class':'form-control'}))
views.py
from .models import Member, Product, Image, User, OrderItem
from .forms import *
def checkout(request):
cart_items = request.session
# Create an empty cart object if it does not exist yet
if not cart_items.has_key("cart"):
cart_items["cart"] = {}
item_ids = cart_items["cart"].keys()
products = Product.objects.filter(pk__in=item_ids)
cart_total = 0
for item in products:
cart_item = cart_items["cart"].get(str(item.id))
cart_total = (item.price * cart_item.get("quantity")) + cart_total
form = OrderCreateForm(request.POST)
if request.method == 'POST':
if form.is_valid():
order = Order.objects.create(
full_name = form.cleaned_data['full_name'],
address = form.cleaned_data['address'],
city = form.cleaned_data['city'],
state = form.cleaned_data['state'],
postcode = form.cleaned_data['postcode'],
phone = form.cleaned_data['phone'],
email = form.cleaned_data['email']
)
for item in products:
orderitem = OrderItem.objects.create(
order = order,
product = item['product'],
price = item['price]',
quantity = item['quantity']
)
cart_items.clear()
order.save()
orderitem.save()
# return HttpResponse(result)
return render(request,'ecommerce/products/')
else:
# return HttpResponse(result)
return render(request, Helpers.get_url('checkout.html'), {'cart_session_items': cart_items["cart"], 'cart_db_items': products, 'cart_total': float("%0.2f" % (cart_total)), 'currency': EcommerceConfig.currency, 'form': OrderCreateForm()})
对不起,我的英语不好,请问我信息是否不清楚 谢谢