【问题标题】:no user matches the given query没有用户匹配给定的查询
【发布时间】: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)
我的产品 models.py 是:

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


    【解决方案1】:

    您的视图的 URL 路由没有为电子邮件指定关键字参数。

    path('userpost/<email>/', UserPostListView.as_view(), name='user-post'),
    

    要早点发现这一点,您可以更改视图的行:

    user = get_object_or_404(User, email=self.kwargs['email'])
    

    当关键字参数未通过 URL 路由提供时,上述内容将引发 KeyError

    【讨论】:

    • 先生,现在它给了我错误,即产品/userpost/khizerkhan736@gmail.com/ 上的 TemplateDoesNotExist。产品/用户帖子,产品/product_list.html
    • 在视图和设置中检查您的模板路径。有东西试图注入您的电子邮件或用户的电子邮件。
    • 我很高兴你能够让它工作!
    猜你喜欢
    • 2017-03-08
    • 2020-10-04
    • 2019-07-21
    • 2021-11-21
    • 2019-12-23
    • 2016-12-05
    • 2015-03-25
    • 1970-01-01
    • 2021-11-21
    相关资源
    最近更新 更多