django making a custom 403 page

To create a custom 403 page in Django, follow these steps:

  1. Create a new HTML template for the custom 403 page. This template will be used to display the content of the page. You can name the template "403.html" or any other suitable name.

  2. Open your Django project's settings.py file and locate the "TEMPLATES" setting. Inside the "TEMPLATES" list, find the "DIRS" option. If "DIRS" is not present, add it under the "TEMPLATES" setting.

  3. In the "DIRS" option, specify the path to the directory where you want to store your custom templates. For example, if you have a templates folder in your Django project's root directory, you can add the following line: 'DIRS': [os.path.join(BASE_DIR, 'templates')],

  4. In the same settings.py file, find the "TEMPLATES" setting again and locate the "APP_DIRS" option. Set it to True. This will ensure that Django searches for templates in all installed apps.

  5. Save the settings.py file.

  6. Go back to your custom 403.html template and add the desired content and styling. You can include any HTML, CSS, or JavaScript code to customize the page according to your needs.

  7. In your Django project's urls.py file, add the following import at the top: from django.views.defaults import permission_denied

  8. Inside the urlpatterns list, add a new URL pattern for the 403 page. For example: urlpatterns = [ # other URL patterns path('403/', permission_denied, kwargs={'exception': Exception('Permission Denied')}, name='403'), ]

  9. Save the urls.py file.

  10. Start your Django server and visit the URL you specified in the urlpatterns list for the 403 page. You should see your custom 403 page displayed.

By following these steps, you will be able to create a custom 403 page in Django.