【发布时间】:2021-05-07 19:27:08
【问题描述】:
大家好,我正在构建一个电子商务 web 应用程序,除此之外一切都已完成。我希望用户获得它自己发布的产品 我的views.py 文件是:
class UserPostListView(ListView):
model = Product
template_name = 'product/user-posts.html'
def get_queryset(self):
user = get_object_or_404(User, email=self.kwargs.get('email'))
return Product.objects.filter(author=user , is_staff=True).order_by('-date_posted')
我的 urls.py 文件是:
urlpatterns = [
# path('featured/' , ProductFeaturedListView.as_view()),
#path('featured/<int:pk>' , ProductFeaturedDetailView.as_view()),
path('' , ProductListView.as_view(), name= "list"),
path('new/' , ProductCreateView.as_view() , name="product-create"),
path('<slug:slug>/update/' , ProductUpdateView.as_view() , name="product-update"),
path('<slug:slug>/delete/' , ProductDeleteView.as_view() , name="product-delete"),
#path('product-fbv/' , product_list_view),
#path('product/<int:pk>' , ProductDetailView.as_view()),
path('<slug:slug>/comment' , CommentCreateView.as_view() , name="add-comment"),
path('<slug:slug>' , ProductDetailSlugView.as_view() , name="detail"),
path('userpost/', UserPostListView.as_view(), name='user-post'),
# path('product-fbv/<int:pk>' , product_detail_view),
]
我的用户模型是:
class User(AbstractBaseUser):
email = models.EmailField(max_length=255 ,unique=True)
full_name = models.CharField(max_length=255, blank=True, null=True)
active = models.BooleanField(default=True) #can login
staff = models.BooleanField(default=False) #staff user non super user
admin = models.BooleanField(default=False) #superuser
time_stamp = models.DateTimeField(auto_now_add=True)
class Product(models.Model):
title = models.CharField(max_length=110)
slug = models.SlugField(blank=True, unique=True)
status = models.CharField(choices=CATEGORY_CHOICES, max_length=10)
price = models.DecimalField(decimal_places=2, max_digits=6)
discount_price=models.FloatField(blank=True, null=True)
size = models.CharField(choices=SIZE_CHOICES, max_length=20)
color = models.CharField(max_length=20, blank=True, null=True)
image = models.ImageField(upload_to=upload_image_path)
description = RichTextField(max_length=1000)
featured = models.BooleanField(default=False)
author = models.ForeignKey(User, on_delete=models.CASCADE)
time_stamp = models.DateTimeField(auto_now_add=True)
它给了我没有用户匹配给定查询的错误
【问题讨论】:
标签: python html django web django-views