User authentication in Django

فهرست فصل‌ها دانلود آرشیوناشر

Django Docs · نسخهٔ 33d2e5948a0b · BSD-3-Clause

=============================

User authentication in Django

:hidden:

default passwords customizing

:synopsis: Django's authentication framework.

Django comes with a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. This section of the documentation explains how the default implementation works out of the box, as well as how to extend and customize it to suit your project's needs.

Overview

The Django authentication system handles both authentication and authorization. Briefly, authentication verifies a user is who they claim to be, and authorization determines what an authenticated user is allowed to do. Here the term authentication is used to refer to both tasks.

The auth system consists of:

  • Users
  • Permissions: Binary (yes/no) flags designating whether a user may perform

a certain task.

  • Groups: A generic way of applying labels and permissions to more than one

user.

  • A configurable password hashing system
  • Forms and view tools for logging in users, or restricting content
  • A pluggable backend system

The authentication system in Django aims to be very generic and doesn't provide some features commonly found in web authentication systems. Solutions for some of these common problems have been implemented in third-party packages:

  • Password strength checking
  • Throttling of login attempts
  • Authentication against third-parties (OAuth, for example)
  • Object-level permissions

Installation

Authentication support is bundled as a Django contrib module in `django.contrib.auth. By default, the required configuration is already included in the settings.py generated by :djadmin:django-admin startproject <startproject>, these consist of two items listed in your INSTALLED_APPS` setting:

  1. `'django.contrib.auth'` contains the core of the authentication framework,

and its default models.

  1. `'django.contrib.contenttypes' is the Django :doc:content type system

</ref/contrib/contenttypes>`, which allows permissions to be associated with models you create.

and these items in your MIDDLEWARE setting:

#. ~django.contrib.sessions.middleware.SessionMiddleware manages sessions across requests. #. ~django.contrib.auth.middleware.AuthenticationMiddleware associates users with requests using sessions.

With these settings in place, running the command `manage.py migrate` creates the necessary database tables for auth related models and permissions for any models defined in your installed apps.

Usage

Using Django's default implementation

  • Working with User objects <user-objects>
  • Permissions and authorization <topic-authorization>
  • Authentication in web requests <auth-web-requests>
  • Managing users in the admin <auth-admin>

API reference for the default implementation

Customizing Users and authentication

Password management in Django