VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-use-permission-required-decorators-with-django-class-based-views/

⇱ How to Use permission_required Decorators with Django Class-Based Views - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Use permission_required Decorators with Django Class-Based Views

Last Updated : 23 Jul, 2025

In Django, permissions are used to control access to views and resources. When working with function-based views (FBVs), decorators like @permission_required are commonly used to restrict access based on user permissions. But what happens when we use class-based views (CBVs)?

Django offers flexibility to apply the same permission checks to CBVs as well. In this article, we will explore how to use the permission_required decorator on Django Class-Based Views. We'll build a small project to demonstrate how to implement these permission checks effectively.

What is permission_required Decorator on Django Class-Based Views?

The permission_required decorator checks if the user has the required permission(s) to access a particular view. If the user doesn't have the necessary permission, they are redirected to the login page or denied access with a 403 (Forbidden) error.

While applying decorators is straightforward for FBVs, using them with CBVs requires the method_decorator utility, which allows us to wrap specific HTTP methods or the entire class in decorators.

Requirements:

  • Python 3.x
  • Django 3.x or 4.x
  • Basic understanding of Django Class-Based Views and User Permissions

How to Use permission_required Decorators on Django Class-Based Views

Step 1: Set up a Django project

To start, create a new Django project and an app to demonstrate the use of the permission_required decorator. Open your terminal and run the following commands:

django-admin startproject permission_demo
cd permission_demo
python manage.py startapp blog

Add the blog app to the INSTALLED_APPS section in permission_demo/settings.py:

πŸ‘ fff
Django Project Structure

Step 2: Define Models

Next, create a simple model in blog/models.py:

Run the following command to create the necessary database tables:

python manage.py makemigrations
python manage.py migrate

Createsuperuser using below command

python manage.py createsuperuser

Step 3: Create Views

In blog/views.py, create the class-based views for listing and creating posts. We'll add permissions for the CreateView.

Step 4: Define URLs

In blog/urls.py, configure the URLs for listing posts and creating new posts:

Step 5: Create Templates

Create a directory called templates/blog/ in your app folder and add the following templates.

post_list.html:

post_form.html:

Step 6: Set Up Permissions

  • Go to the Django admin panel at http://127.0.0.1:8000/admin/.
  • Under "Users," select a user and assign the "Can add post" permission under the "blog" app.
  • Now, this user will have the permission to create posts and edit post

Step 7: Run the Project

Start the development server:

python manage.py runserver

Now, visit the following URLs:

  • http://127.0.0.1:8000/ – Displays the post list and a link to create a new post (if the user has the required permission).
  • http://127.0.0.1:8000/post/new/ – Redirects to the login page or shows a permission denied error if the user doesn’t have the "add post" permission.

Output:

Conclusion

In this article, we covered how to use the permission_required decorator on Django class-based views. We demonstrated how to apply it using the method_decorator utility for class-based views and built a small project to see it in action. This pattern allows us to manage user permissions effectively, ensuring that only authorized users can access certain views or perform specific actions like creating new posts.

By extending this concept, we can add more permission checks for other actions like updating or deleting records, offering granular control over who can interact with our app's resources.

Comment
Article Tags: