进行身份验证并授予用户一定的权限,具体取决于用户的类型.但是注意正在发生 [英] Traying to authentication and give user certain permission, depending on the type of user. But noting is happening

查看:59
本文介绍了进行身份验证并授予用户一定的权限,具体取决于用户的类型.但是注意正在发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进行身份验证并授予用户某些权限,具体取决于用户的类型.但是注意正在发生.我尝试根据用户类型来限制对网站上某些功能的访问.但是,当我设置条件时,页面上什么也没有发生:这是我的模型.py

 from django.contrib.auth.models import AbstractUser
 from django.db import models
 from django.urls import reverse

 user_type_choice = (('1', 'majstor'),
                    ('2', 'korisnik'))

 class CustomKorisnici(AbstractUser):
     user_type = models.CharField(max_length=100,blank=True,choices=user_type_choice)
     username = models.CharField(max_length=100,unique=True)
     last_name = models.CharField(max_length=100)
     first_name = models.CharField(max_length=100)
     phone_number = models.CharField(max_length=100)
     is_superuser = models.BooleanField(default=False)
     is_active = models.BooleanField(default=True)
     is_staff = models.BooleanField(default=False)
     email = models.EmailField(max_length=100,unique=True)

在设置中,我设置了:AUTH_USER_MODEL.

 AUTH_USER_MODEL ='korisnici.CustomKorisnici' 

这是我的login.html页面.这部分工作正常.

 {% extends "nav_footer.html" %} 
 {% load static %}
 {% block content %}
 <div class="form-group">
   <div class="container">
     <div class="form">
       <form method="post">     
       {% csrf_token %}
       {{ form.as_p }}
       <button id="btn" class="btn" type="submit">Login</button>
       </form>
     </div>
   </div>
  </div>
 </div>
 {% endblock %}

**在我的home.html页面中,我为用户设置了条件,这是一个问题.**

**In my home.html page, I set condition for Users and here is a problem. **

   {% if user.is_authenticated and user.user_type == "korisnik" %}
      <div class="col-md-4">
         <a class="nav-link" href="{% url 'post_page' %}">All posts</a>
       </div>
   {% endif %}

首先,我设置了一个条件,如果user.is_authenticated可以正常工作.之后,仅用于检查,如果user.is_authenticated和user.username =='admin',则添加一个条件.当我以管理员身份或用户名=='John'的其他某种条件登录时​​,它工作正常并且链接可见.但是,当我尝试条件user.user_type =="korisnik"时,即使我登录用户,如何将user_type设置为korisnik,链接也不可见.我不知道我在做什么错.我需要做自定义登录功能还是其他操作

推荐答案

存储在数据库中的值是元组的第一个值.从元组('1','majstor')的含义中,第一个值'1'将存储在字段中,以供'majstor'.因此,您应该在模板中编写:

The value stored in the database is the first value of the tuple. Meaning from the tuple ('1', 'majstor') the first value '1' would be stored in the field for users of type 'majstor'. So in your template you should be writing:

{% if user.is_authenticated and user.user_type == "2" %}

使检查变得容易的最好方法是在模型中使用常量.因此,您将像这样更改模型:

Also to make checking easy the best thing to do is to use constants in your model. So you would change your model like so:

class CustomKorisnici(AbstractUser):
    MAJSTOR = '1'
    KORISNIK = '2'
    USER_TYPE_CHOICE = (
        (MAJSTOR, 'majstor'),
        (KORISNIK, 'korisnik')
     )
    user_type = models.CharField(max_length=100, blank=True, choices=USER_TYPE_CHOICE)
    # rest of the fields etc.

现在在模板中检查将变成:

Now in the template checking would simply become:

{% if user.is_authenticated and user.user_type == user.KORISNIK %}

这篇关于进行身份验证并授予用户一定的权限,具体取决于用户的类型.但是注意正在发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆