Django Tutorial

Django Create A Complete Project

Django Template

Django Database Query

Django Form

Django Authentication and Permission Management

Django unittest

Django Advanced

Django Auth Users And User Groups

In this tutorial, we'll cover how to manage users and user groups in a Django application using Django's built-in authentication system.

Prerequisites:

  • Basic knowledge of Python
  • Familiarity with Django framework
  • A Django project set up and running with Django's authentication system enabled (refer to the previous tutorials)

Step 1: Set up Django authentication

If you haven't set up Django authentication yet, refer to the previous tutorials for the initial steps. Make sure you have the following apps in the INSTALLED_APPS list in your myproject/settings.py:

INSTALLED_APPS = [
    # ...
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'myapp',
]

Step 2: Create superuser

2.1. Create a superuser account if you haven't already:

python manage.py createsuperuser

Step 3: Access the Django admin site

3.1. Run the development server:

python manage.py runserver

3.2. Open the Django admin site (http://127.0.0.1:8000/admin/) in your web browser and log in with your superuser account.

Step 4: Create and manage users

4.1. In the Django admin site, click on "Users" under the "Authentication and Authorization" section.

4.2. Click on the "Add user" button in the top-right corner to create a new user. Fill in the required fields and click "Save".

4.3. Click on the newly created user to edit their information, set a password, assign them to groups, or grant them specific permissions.

Step 5: Create and manage user groups

5.1. In the Django admin site, click on "Groups" under the "Authentication and Authorization" section.

5.2. Click on the "Add group" button in the top-right corner to create a new group.

5.3. Give the group a name, then select the permissions you want to assign to this group.

5.4. Click "Save" to create the group.

5.5. Now you can add users to the group, which will grant them the permissions assigned to the group. To do this, go back to the user's edit page and select the group(s) you want to add them to.

Step 6: Test user permissions and groups

6.1. Log out of the admin site, then log in with one of the new users you created.

6.2. Test the permissions of the user by trying to access different parts of the admin site. Users with the necessary permissions should be able to perform specific actions, while users without the permissions should be denied access.

6.3. If you've assigned a user to a group, make sure the user inherits the permissions of the group.

And that's it! You've successfully managed users and user groups using Django's built-in authentication system. You can now use these user accounts to manage access and permissions throughout your application.

  1. Adding users to groups in Django: You can add users to groups using the groups attribute of the User model or through the Django admin interface.

    # models.py
    from django.contrib.auth.models import User, Group
    
    user = User.objects.get(username='example_user')
    group = Group.objects.get(name='example_group')
    user.groups.add(group)
    
  2. Customizing Django user model and groups: You can customize the User model or create a custom user model to include additional fields or behaviors.

    # models.py
    from django.contrib.auth.models import AbstractUser
    
    class CustomUser(AbstractUser):
        # Custom fields or methods here
    
  3. Django auth app group permissions: Permissions can be assigned to groups in the Django admin interface or programmatically.

    # models.py
    from django.contrib.auth.models import Group, Permission
    
    group = Group.objects.get(name='example_group')
    permission = Permission.objects.get(codename='can_do_something')
    group.permissions.add(permission)
    
  4. Querying user groups in Django: You can retrieve the groups associated with a user or list all groups in the system.

    # views.py
    from django.contrib.auth.models import User
    
    user = User.objects.get(username='example_user')
    user_groups = user.groups.all()