Search code examples
djangodjango-modelsdjango-formsdjango-authenticationdjango-login

Traying to authentication and give user certain permission, depending on the type of user. But noting is happening


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


Solution

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