[英]Only staff user should post a product in django
I am working with an ecommerce app where everone who login can post their product but I want only staff user to post the product when the staff user is true: my models.py is:我正在使用一个电子商务应用程序,登录的每个人都可以发布他们的产品,但我只希望员工用户在员工用户为真时发布产品:我的 models.py 是:
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)
#USERNAME_FIELD and password are required by default
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
object = UserManager()
def __str__(self):
return self.email
def get_full_name(self):
if self.full_name:
return self.full_name
return self.email
def get_short_name(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self , app_label):
return True
@property
def is_staff(self):
return self.staff
@property
def is_admin(self):
return self.admin
@property
def is_active(self):
return self.active
and my views.py for creating product is:我用于创建产品的 views.py 是:
class ProductCreateView(LoginRequiredMixin,CreateView):
model = Product
template_name = 'product-form.html'
fields = ['title', 'price' , 'status' , 'size' , 'color', 'image' , 'description']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
Also I want that if staff is true then navbar show create product for user.我也希望如果员工是真的,那么导航栏显示为用户创建产品。 right now if user is authenticated navbar show him to post the product现在,如果用户经过身份验证,导航栏显示他发布产品
{% if request.user.is_authenticated %} <li class="nav-item {% if request.path == logout_url%}active{% endif %}"> <a class="nav-link" href="{{ logout_url }}">Logout</a> </li> <li class="nav-item"> <a class="nav-link" href="{% url 'profile'%}">Profile</a> </li> <li class="nav-item"> <a class="nav-link" href="{% url 'products:product-create'%}">Create Product</a> </li> {% else%}
You can use the UserPassesTestMixin
in your view like this.您可以像这样在视图中使用UserPassesTestMixin
。
class ProductCreateView(UserPassesTestMixin,CreateView):
model = Product
template_name = 'product-form.html'
fields = ['title', 'price' , 'status' , 'size' , 'color', 'image' , 'description']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
return self.request.user.is_staff
And in the template you can test with this {% if request.user.is_staff %}
在模板中,您可以使用此{% if request.user.is_staff %}
进行测试
You should checkout the staff_member_required decorator and as already answered, use {% if user.is_staff %}
in your template for the navigation.您应该检查staff_member_required装饰器,并且已经回答,在您的模板中使用{% if user.is_staff %}
进行导航。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.