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

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

问题描述

根据用户类型进行身份验证并授予用户一定的权限.但是注意正在发生.我尝试根据用户类型限制对网站某些功能的访问.但是当我设置条件时,页面上没有任何反应:这是我的model.py

 from django.contrib.auth.models import AbstractUser从 django.db 导入模型从 django.urls 导入反向user_type_choice = (('1', 'majstor'),('2', 'korisnik'))类 CustomKorisnici(AbstractUser):user_type = models.CharField(max_length=100,blank=True,choices=user_type_choice)用户名 = 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(默认=真)is_staff = models.BooleanField(默认=假)email = models.EmailField(max_length=100,unique=True)

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

 AUTH_USER_MODEL ='korisnici.CustomKorisnici'

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

 {% extends "nav_footer.html";%}{% 加载静态 %}{% 块内容 %}<div class="form-group"><div class="container"><div class="form"><表单方法=发布">{% csrf_token %}{{ form.as_p }}<button id="btn";类=btn"type=提交">登录</表单>

{% 结束块 %}

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

 {% if user.is_authenticated and user.user_type == "korisnik";%}<div class="col-md-4"><a class="nav-link";href={% url 'post_page' %}">所有帖子</a>

{% 万一 %}

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

解决方案

存储在数据库中的值是元组的第一个值.元组 ('1', 'majstor') 的含义是,第一个值 '1' 将存储在 'majstor'<类型的用户的字段中/代码>.所以在你的模板中你应该写:

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

另外为了使检查变得容易,最好的办法是在模型中使用常量.所以你会像这样改变你的模型:

class CustomKorisnici(AbstractUser):大师 = '1'科里斯尼克 = '2'USER_TYPE_CHOICE = ((MAJSTOR, 'majstor'),(KORISNIK, 'korisnik'))user_type = models.CharField(max_length=100,空白=真,选择=USER_TYPE_CHOICE)# 其余字段等

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

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

Traying to authentication and give user certain permission, depending on the type of user. But noting is happening. I try to limit access to certain functions on the site, depending on the type of user. But when I set a condition, nothing happens on the page: This is my model.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)

In the settings, I set: AUTH_USER_MODEL.

 AUTH_USER_MODEL ='korisnici.CustomKorisnici' 

this is my login.html page. This part works ok.

 {% 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 %}

**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 %}

First I set a condition if user.is_authenticated and this is working fine. After that just for checking, I add a condition if user.is_authenticated and user.username == 'admin'. When I log in as Admin or some other condition for username == 'John', it is working fine and link is visible. But when I try condition user.user_type == "korisnik", link is not visible even when I login whit User how is user_type set to be korisnik. I don't know what am I doing wrong here. Do I need to do custom login function or something else

解决方案

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天全站免登陆