【问题标题】:How to prevent user from creating new content until currently created content is verified by admin如何在管理员验证当前创建的内容之前阻止用户创建新内容
【发布时间】:2022-01-06 12:54:36
【问题描述】:

所以我有这个 CreateShopView 视图供卖家类型用户简单地注册他们的商店。 Shop 模型有一个状态字段,默认情况下设置为“处理”,显然我没有在我的 ShopCreateForm 中包含该字段,因此每次卖家注册商店时,它都会以“处理”状态保存在数据库中。 我的问题是如何防止卖家在他们仍然拥有“正在处理”状态的商店时注册新商店。提前致谢。

【问题讨论】:

    标签: django django-models django-views django-templates


    【解决方案1】:

    您可能甚至不想让用户访问表单!

    你的 view.py 中的这些内容应该可以工作:

    from django.contrib import messages
    
    def new_shop(request):
      shop = Shop.objects.filter(user=request.user).last()
    
      if shop:
        if shop.status == 'processing':
          messages.add(request, messages.ERROR, 'Your last shop has not been approved yet.')
          return redirect('shop:overview') # redirect the user and display error message
    
      # render the form here
    
    

    【讨论】:

    • 在我的代码中,视图是基于类的视图,我假设我必须在我的 CreateShopView 类中将其作为自定义方法来实现,对吗?
    猜你喜欢
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2018-06-30
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多