无论何时尝试将商品添加到购物车,它都不会显示在购物车中。没有错误,模板也可以正常工作,但是此商品中的http://127.0.0.1:8000/admin/ecom/orderitem/没有显示。 我正试图从三天之内解决这个问题,但是它不起作用。
from django.shortcuts import render, get_object_or_404, redirect
from .models import Item, Checkout, Order, Orderitem
from django.urls import reverse_lazy
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView
class CheckoutEditView( CreateView ) :
model = Checkout
template_name = 'checkedit.html'
fields = '__all__'
success_url = reverse_lazy('blank')
def item_list(request):
context = {
'items':Item.objects.all()
}
return render(request,'item_list.html',context)
class CheckListView(ListView):
model = Checkout
template_name = 'blank.html'
def check2(request):
return render(request,'ckeck2.html')
def front(request):
return render(request,'front.html')
def blank(request):
return render(request,'blank.html')
def product(request):
return render(request,'product-page.html')
def checkout(request):
return render(request,'checkout-page.html')
def home(request):
return render(request,'home.html')
class ArticleDetailView(DetailView):
model = Item
template_name = 'home.html'
context_object_name = 'batman'
def home_page(request):
return render(request,'home-page.html')
def add_to_cart(request,slug):
item = get_object_or_404(Item,slug=slug)
order_item = Orderitem.objects.create(item=item)
order_qs = Order.objects.filter(user=request.user,ordered = False)
if order_qs.exists():
order = order_qs[0]
if order.items.filter(item__slug=item.slug).exists():
order_item.quantity +=1
order_item.save()
else:
order = Order.objects.create(user=request.user)
order.items.add(order_item)
return redirect("core:article_page",slug=slug)
无论何时尝试将商品添加到购物车,它都不会显示在购物车中。没有错误,模板也可以正常工作,但是此商品中的http://127.0.0.1:8000/admin/ecom/orderitem/没有显示。 我正试图从三天之内解决这个问题,但是这很正常。
from django.db import models
from django.conf import settings
from django.urls import reverse
class Item(models.Model):
title = models.CharField(max_length=100)
pic = models.ImageField(upload_to='hero_headshots/')
discription = models.TextField()
price = models.IntegerField()
slug = models.SlugField()
discount = models.FloatField(blank=True,null=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("core:article_page",kwargs={
'slug': self.slug})
def get_add_to_cart_url(self):
return reverse("core:add-to-cart", kwargs={
'slug': self.slug
})
class Orderitem(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
def __str__(self):
return f"{self.quantity} of {self.item.title}"
class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
items = models.ManyToManyField(Orderitem)
start_date = models.DateTimeField(auto_now_add=True)
ordered = models.BooleanField(default=False)
def __str__(self):
return self.user.username
class Checkout(models.Model):
Full_Name = models.CharField(max_length=15)
Email = models.EmailField()
Address = models.TextField()
phone = models.CharField(max_length=13)
def __str__(self):
return self.Email