how to handle authentication in django for multiple apps and functionality

Viewed 50

I know authentication should be global. all apps in a django project should serve the same purpose, but I am in a situation where I need to restrict some parts of the app to some users.

I have a screen that allows you to access different app functionality

.i.e

  • personal spending
  • to-do list
  • e-mail scheduler/ report scheduler

users should be able to see all apps, click on them, log in and then access different functionalities based on who they are.

the views should also be different when they're an admin or just a simple user.

how can this be achieved in django? is it with groups ?

1 Answers

You can use different thing to achieve this.

Using decorators, for example, if you want any user to see something but can't edit those. You can verify their level of permission when rendering the page, so they either can or cannot access it. And yes, to do so, you have to define groups of users and set permissions. To see all the permissions possible in your app, you can look at the table called auth_permission in your database.

If you know exactly the user you want to have access to a certain part of your app, another solution would be something like :

def username_check(user):
   return user.email.startswith('linux')

and use it in your view like :

check = username_check(user_)
    if check:
        # do something or get an object to send to the template
    else:
        # do something else

and in your template, you can do something like:

{% if objects_that_you_passed_via_context_is_not_none %}
      # show the user something             
{% else %}
      # show something else                         
{% endif %}
Related