doc_content
stringlengths
1
386k
doc_id
stringlengths
5
188
class ModelBackend This is the default authentication backend used by Django. It authenticates using credentials consisting of a user identifier and password. For Django’s default user model, the user identifier is the username, for custom user models it is the field specified by USERNAME_FIELD (see Customizing Users...
django.ref.contrib.auth#django.contrib.auth.backends.ModelBackend
authenticate(request, username=None, password=None, **kwargs) Tries to authenticate username with password by calling User.check_password. If no username is provided, it tries to fetch a username from kwargs using the key CustomUser.USERNAME_FIELD. Returns an authenticated user or None. request is an HttpRequest and ...
django.ref.contrib.auth#django.contrib.auth.backends.ModelBackend.authenticate
get_all_permissions(user_obj, obj=None) Returns the set of permission strings the user_obj has, including both user permissions and group permissions. Returns an empty set if is_anonymous or is_active is False.
django.ref.contrib.auth#django.contrib.auth.backends.ModelBackend.get_all_permissions
get_group_permissions(user_obj, obj=None) Returns the set of permission strings the user_obj has from the permissions of the groups they belong. Returns an empty set if is_anonymous or is_active is False.
django.ref.contrib.auth#django.contrib.auth.backends.ModelBackend.get_group_permissions
get_user_permissions(user_obj, obj=None) Returns the set of permission strings the user_obj has from their own user permissions. Returns an empty set if is_anonymous or is_active is False.
django.ref.contrib.auth#django.contrib.auth.backends.ModelBackend.get_user_permissions
has_module_perms(user_obj, app_label) Returns whether the user_obj has any permissions on the app app_label.
django.ref.contrib.auth#django.contrib.auth.backends.ModelBackend.has_module_perms
has_perm(user_obj, perm, obj=None) Uses get_all_permissions() to check if user_obj has the permission string perm. Returns False if the user is not is_active.
django.ref.contrib.auth#django.contrib.auth.backends.ModelBackend.has_perm
user_can_authenticate() Returns whether the user is allowed to authenticate. To match the behavior of AuthenticationForm which prohibits inactive users from logging in, this method returns False for users with is_active=False. Custom user models that don’t have an is_active field are allowed.
django.ref.contrib.auth#django.contrib.auth.backends.ModelBackend.user_can_authenticate
with_perm(perm, is_active=True, include_superusers=True, obj=None) Returns all active users who have the permission perm either in the form of "<app label>.<permission codename>" or a Permission instance. Returns an empty queryset if no users who have the perm found. If is_active is True (default), returns only activ...
django.ref.contrib.auth#django.contrib.auth.backends.ModelBackend.with_perm
class RemoteUserBackend Use this backend to take advantage of external-to-Django-handled authentication. It authenticates using usernames passed in request.META['REMOTE_USER']. See the Authenticating against REMOTE_USER documentation. If you need more control, you can create your own authentication backend that inher...
django.ref.contrib.auth#django.contrib.auth.backends.RemoteUserBackend
authenticate(request, remote_user) The username passed as remote_user is considered trusted. This method returns the user object with the given username, creating a new user object if create_unknown_user is True. Returns None if create_unknown_user is False and a User object with the given username is not found in th...
django.ref.contrib.auth#django.contrib.auth.backends.RemoteUserBackend.authenticate
clean_username(username) Performs any cleaning on the username (e.g. stripping LDAP DN information) prior to using it to get or create a user object. Returns the cleaned username.
django.ref.contrib.auth#django.contrib.auth.backends.RemoteUserBackend.clean_username
configure_user(request, user) Configures a newly created user. This method is called immediately after a new user is created, and can be used to perform custom setup actions, such as setting the user’s groups based on attributes in an LDAP directory. Returns the user object. request is an HttpRequest and may be None ...
django.ref.contrib.auth#django.contrib.auth.backends.RemoteUserBackend.configure_user
create_unknown_user True or False. Determines whether or not a user object is created if not already in the database Defaults to True.
django.ref.contrib.auth#django.contrib.auth.backends.RemoteUserBackend.create_unknown_user
user_can_authenticate() Returns whether the user is allowed to authenticate. This method returns False for users with is_active=False. Custom user models that don’t have an is_active field are allowed.
django.ref.contrib.auth#django.contrib.auth.backends.RemoteUserBackend.user_can_authenticate
auth()
django.ref.templates.api#django.contrib.auth.context_processors.auth
login_required(redirect_field_name='next', login_url=None) As a shortcut, you can use the convenient login_required() decorator: from django.contrib.auth.decorators import login_required @login_required def my_view(request): ... login_required() does the following: If the user isn’t logged in, redirect to sett...
django.topics.auth.default#django.contrib.auth.decorators.login_required
permission_required(perm, login_url=None, raise_exception=False) It’s a relatively common task to check whether a user has a particular permission. For that reason, Django provides a shortcut for that case: the permission_required() decorator.: from django.contrib.auth.decorators import permission_required @permissi...
django.topics.auth.default#django.contrib.auth.decorators.permission_required
user_passes_test(test_func, login_url=None, redirect_field_name='next') As a shortcut, you can use the convenient user_passes_test decorator which performs a redirect when the callable returns False: from django.contrib.auth.decorators import user_passes_test def email_check(user): return user.email.endswith('@e...
django.topics.auth.default#django.contrib.auth.decorators.user_passes_test
class AdminPasswordChangeForm A form used in the admin interface to change a user’s password. Takes the user as the first positional argument.
django.topics.auth.default#django.contrib.auth.forms.AdminPasswordChangeForm
class AuthenticationForm A form for logging a user in. Takes request as its first positional argument, which is stored on the form instance for use by sub-classes. confirm_login_allowed(user) By default, AuthenticationForm rejects users whose is_active flag is set to False. You may override this behavior with a c...
django.topics.auth.default#django.contrib.auth.forms.AuthenticationForm
confirm_login_allowed(user) By default, AuthenticationForm rejects users whose is_active flag is set to False. You may override this behavior with a custom policy to determine which users can log in. Do this with a custom form that subclasses AuthenticationForm and overrides the confirm_login_allowed() method. This m...
django.topics.auth.default#django.contrib.auth.forms.AuthenticationForm.confirm_login_allowed
class PasswordChangeForm A form for allowing a user to change their password.
django.topics.auth.default#django.contrib.auth.forms.PasswordChangeForm
class PasswordResetForm A form for generating and emailing a one-time use link to reset a user’s password. send_mail(subject_template_name, email_template_name, context, from_email, to_email, html_email_template_name=None) Uses the arguments to send an EmailMultiAlternatives. Can be overridden to customize how th...
django.topics.auth.default#django.contrib.auth.forms.PasswordResetForm
send_mail(subject_template_name, email_template_name, context, from_email, to_email, html_email_template_name=None) Uses the arguments to send an EmailMultiAlternatives. Can be overridden to customize how the email is sent to the user. Parameters: subject_template_name – the template for the subject. email_te...
django.topics.auth.default#django.contrib.auth.forms.PasswordResetForm.send_mail
class SetPasswordForm A form that lets a user change their password without entering the old password.
django.topics.auth.default#django.contrib.auth.forms.SetPasswordForm
class UserChangeForm A form used in the admin interface to change a user’s information and permissions.
django.topics.auth.default#django.contrib.auth.forms.UserChangeForm
class UserCreationForm A ModelForm for creating a new user. It has three fields: username (from the user model), password1, and password2. It verifies that password1 and password2 match, validates the password using validate_password(), and sets the user’s password using set_password().
django.topics.auth.default#django.contrib.auth.forms.UserCreationForm
get_user(request) Returns the user model instance associated with the given request’s session. It checks if the authentication backend stored in the session is present in AUTHENTICATION_BACKENDS. If so, it uses the backend’s get_user() method to retrieve the user model instance and then verifies the session by callin...
django.ref.contrib.auth#django.contrib.auth.get_user
get_user_model() Instead of referring to User directly, you should reference the user model using django.contrib.auth.get_user_model(). This method will return the currently active user model – the custom user model if one is specified, or User otherwise. When you define a foreign key or many-to-many relations to the...
django.topics.auth.customizing#django.contrib.auth.get_user_model
check_password(password, encoded) If you’d like to manually authenticate a user by comparing a plain-text password to the hashed password in the database, use the convenience function check_password(). It takes two arguments: the plain-text password to check, and the full value of a user’s password field in the datab...
django.topics.auth.passwords#django.contrib.auth.hashers.check_password
is_password_usable(encoded_password) Returns False if the password is a result of User.set_unusable_password().
django.topics.auth.passwords#django.contrib.auth.hashers.is_password_usable
make_password(password, salt=None, hasher='default') Creates a hashed password in the format used by this application. It takes one mandatory argument: the password in plain-text (string or bytes). Optionally, you can provide a salt and a hashing algorithm to use, if you don’t want to use the defaults (first entry of...
django.topics.auth.passwords#django.contrib.auth.hashers.make_password
is_active Returns True if the user account is currently active.
django.topics.auth.customizing#django.contrib.auth.is_active
is_staff Returns True if the user is allowed to have access to the admin site.
django.topics.auth.customizing#django.contrib.auth.is_staff
login(request, user, backend=None) To log a user in, from a view, use login(). It takes an HttpRequest object and a User object. login() saves the user’s ID in the session, using Django’s session framework. Note that any data set during the anonymous session is retained in the session after a user logs in. This examp...
django.topics.auth.default#django.contrib.auth.login
logout(request) To log out a user who has been logged in via django.contrib.auth.login(), use django.contrib.auth.logout() within your view. It takes an HttpRequest object and has no return value. Example: from django.contrib.auth import logout def logout_view(request): logout(request) # Redirect to a succes...
django.topics.auth.default#django.contrib.auth.logout
class AuthenticationMiddleware
django.ref.middleware#django.contrib.auth.middleware.AuthenticationMiddleware
class PersistentRemoteUserMiddleware
django.ref.middleware#django.contrib.auth.middleware.PersistentRemoteUserMiddleware
class RemoteUserMiddleware
django.ref.middleware#django.contrib.auth.middleware.RemoteUserMiddleware
class AccessMixin login_url Default return value for get_login_url(). Defaults to None in which case get_login_url() falls back to settings.LOGIN_URL. permission_denied_message Default return value for get_permission_denied_message(). Defaults to an empty string. redirect_field_name Default return val...
django.topics.auth.default#django.contrib.auth.mixins.AccessMixin
get_login_url() Returns the URL that users who don’t pass the test will be redirected to. Returns login_url if set, or settings.LOGIN_URL otherwise.
django.topics.auth.default#django.contrib.auth.mixins.AccessMixin.get_login_url
get_permission_denied_message() When raise_exception is True, this method can be used to control the error message passed to the error handler for display to the user. Returns the permission_denied_message attribute by default.
django.topics.auth.default#django.contrib.auth.mixins.AccessMixin.get_permission_denied_message
get_redirect_field_name() Returns the name of the query parameter that will contain the URL the user should be redirected to after a successful login. If you set this to None, a query parameter won’t be added. Returns the redirect_field_name attribute by default.
django.topics.auth.default#django.contrib.auth.mixins.AccessMixin.get_redirect_field_name
handle_no_permission() Depending on the value of raise_exception, the method either raises a PermissionDenied exception or redirects the user to the login_url, optionally including the redirect_field_name if it is set.
django.topics.auth.default#django.contrib.auth.mixins.AccessMixin.handle_no_permission
login_url Default return value for get_login_url(). Defaults to None in which case get_login_url() falls back to settings.LOGIN_URL.
django.topics.auth.default#django.contrib.auth.mixins.AccessMixin.login_url
permission_denied_message Default return value for get_permission_denied_message(). Defaults to an empty string.
django.topics.auth.default#django.contrib.auth.mixins.AccessMixin.permission_denied_message
raise_exception If this attribute is set to True, a PermissionDenied exception is raised when the conditions are not met. When False (the default), anonymous users are redirected to the login page.
django.topics.auth.default#django.contrib.auth.mixins.AccessMixin.raise_exception
redirect_field_name Default return value for get_redirect_field_name(). Defaults to "next".
django.topics.auth.default#django.contrib.auth.mixins.AccessMixin.redirect_field_name
class LoginRequiredMixin If a view is using this mixin, all requests by non-authenticated users will be redirected to the login page or shown an HTTP 403 Forbidden error, depending on the raise_exception parameter. You can set any of the parameters of AccessMixin to customize the handling of unauthorized users: from ...
django.topics.auth.default#django.contrib.auth.mixins.LoginRequiredMixin
class PermissionRequiredMixin This mixin, just like the permission_required decorator, checks whether the user accessing a view has all given permissions. You should specify the permission (or an iterable of permissions) using the permission_required parameter: from django.contrib.auth.mixins import PermissionRequire...
django.topics.auth.default#django.contrib.auth.mixins.PermissionRequiredMixin
get_permission_required() Returns an iterable of permission names used by the mixin. Defaults to the permission_required attribute, converted to a tuple if necessary.
django.topics.auth.default#django.contrib.auth.mixins.PermissionRequiredMixin.get_permission_required
has_permission() Returns a boolean denoting whether the current user has permission to execute the decorated view. By default, this returns the result of calling has_perms() with the list of permissions returned by get_permission_required().
django.topics.auth.default#django.contrib.auth.mixins.PermissionRequiredMixin.has_permission
class UserPassesTestMixin When using class-based views, you can use the UserPassesTestMixin to do this. test_func() You have to override the test_func() method of the class to provide the test that is performed. Furthermore, you can set any of the parameters of AccessMixin to customize the handling of unauthorize...
django.topics.auth.default#django.contrib.auth.mixins.UserPassesTestMixin
get_test_func() You can also override the get_test_func() method to have the mixin use a differently named function for its checks (instead of test_func()).
django.topics.auth.default#django.contrib.auth.mixins.UserPassesTestMixin.get_test_func
test_func() You have to override the test_func() method of the class to provide the test that is performed. Furthermore, you can set any of the parameters of AccessMixin to customize the handling of unauthorized users: from django.contrib.auth.mixins import UserPassesTestMixin class MyView(UserPassesTestMixin, View)...
django.topics.auth.default#django.contrib.auth.mixins.UserPassesTestMixin.test_func
class models.AbstractBaseUser get_username() Returns the value of the field nominated by USERNAME_FIELD. clean() Normalizes the username by calling normalize_username(). If you override this method, be sure to call super() to retain the normalization. classmethod get_email_field_name() Returns the nam...
django.topics.auth.customizing#django.contrib.auth.models.AbstractBaseUser
check_password(raw_password) Returns True if the given raw string is the correct password for the user. (This takes care of the password hashing in making the comparison.)
django.topics.auth.customizing#django.contrib.auth.models.AbstractBaseUser.check_password
clean() Normalizes the username by calling normalize_username(). If you override this method, be sure to call super() to retain the normalization.
django.topics.auth.customizing#django.contrib.auth.models.AbstractBaseUser.clean
get_session_auth_hash() Returns an HMAC of the password field. Used for Session invalidation on password change.
django.topics.auth.customizing#django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash
get_username() Returns the value of the field nominated by USERNAME_FIELD.
django.topics.auth.customizing#django.contrib.auth.models.AbstractBaseUser.get_username
has_usable_password() Returns False if set_unusable_password() has been called for this user.
django.topics.auth.customizing#django.contrib.auth.models.AbstractBaseUser.has_usable_password
is_anonymous Read-only attribute which is always False. This is a way of differentiating User and AnonymousUser objects. Generally, you should prefer using is_authenticated to this attribute.
django.topics.auth.customizing#django.contrib.auth.models.AbstractBaseUser.is_anonymous
is_authenticated Read-only attribute which is always True (as opposed to AnonymousUser.is_authenticated which is always False). This is a way to tell if the user has been authenticated. This does not imply any permissions and doesn’t check if the user is active or has a valid session. Even though normally you will ch...
django.topics.auth.customizing#django.contrib.auth.models.AbstractBaseUser.is_authenticated
set_password(raw_password) Sets the user’s password to the given raw string, taking care of the password hashing. Doesn’t save the AbstractBaseUser object. When the raw_password is None, the password will be set to an unusable password, as if set_unusable_password() were used.
django.topics.auth.customizing#django.contrib.auth.models.AbstractBaseUser.set_password
set_unusable_password() Marks the user as having no password set. This isn’t the same as having a blank string for a password. check_password() for this user will never return True. Doesn’t save the AbstractBaseUser object. You may need this if authentication for your application takes place against an existing exter...
django.topics.auth.customizing#django.contrib.auth.models.AbstractBaseUser.set_unusable_password
class models.AbstractUser clean() Normalizes the email by calling BaseUserManager.normalize_email(). If you override this method, be sure to call super() to retain the normalization.
django.topics.auth.customizing#django.contrib.auth.models.AbstractUser
clean() Normalizes the email by calling BaseUserManager.normalize_email(). If you override this method, be sure to call super() to retain the normalization.
django.topics.auth.customizing#django.contrib.auth.models.AbstractUser.clean
class models.AnonymousUser django.contrib.auth.models.AnonymousUser is a class that implements the django.contrib.auth.models.User interface, with these differences: id is always None. username is always the empty string. get_username() always returns the empty string. is_anonymous is True instead of False. is_...
django.ref.contrib.auth#django.contrib.auth.models.AnonymousUser
class models.BaseUserManager classmethod normalize_email(email) Normalizes email addresses by lowercasing the domain portion of the email address. get_by_natural_key(username) Retrieves a user instance using the contents of the field nominated by USERNAME_FIELD. make_random_password(length=10, allowed_c...
django.topics.auth.customizing#django.contrib.auth.models.BaseUserManager
get_by_natural_key(username) Retrieves a user instance using the contents of the field nominated by USERNAME_FIELD.
django.topics.auth.customizing#django.contrib.auth.models.BaseUserManager.get_by_natural_key
make_random_password(length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789') Returns a random password with the given length and given string of allowed characters. Note that the default value of allowed_chars doesn’t contain letters that can cause user confusion, including: i, l, I, and...
django.topics.auth.customizing#django.contrib.auth.models.BaseUserManager.make_random_password
class models.CustomUser USERNAME_FIELD A string describing the name of the field on the user model that is used as the unique identifier. This will usually be a username of some kind, but it can also be an email address, or any other unique identifier. The field must be unique (i.e., have unique=True set in its d...
django.topics.auth.customizing#django.contrib.auth.models.CustomUser
EMAIL_FIELD A string describing the name of the email field on the User model. This value is returned by get_email_field_name().
django.topics.auth.customizing#django.contrib.auth.models.CustomUser.EMAIL_FIELD
get_full_name() Optional. A longer formal identifier for the user such as their full name. If implemented, this appears alongside the username in an object’s history in django.contrib.admin.
django.topics.auth.customizing#django.contrib.auth.models.CustomUser.get_full_name
get_short_name() Optional. A short, informal identifier for the user such as their first name. If implemented, this replaces the username in the greeting to the user in the header of django.contrib.admin.
django.topics.auth.customizing#django.contrib.auth.models.CustomUser.get_short_name
is_active A boolean attribute that indicates whether the user is considered “active”. This attribute is provided as an attribute on AbstractBaseUser defaulting to True. How you choose to implement it will depend on the details of your chosen auth backends. See the documentation of the is_active attribute on the built...
django.topics.auth.customizing#django.contrib.auth.models.CustomUser.is_active
REQUIRED_FIELDS A list of the field names that will be prompted for when creating a user via the createsuperuser management command. The user will be prompted to supply a value for each of these fields. It must include any field for which blank is False or undefined and may include additional fields you want prompted...
django.topics.auth.customizing#django.contrib.auth.models.CustomUser.REQUIRED_FIELDS
USERNAME_FIELD A string describing the name of the field on the user model that is used as the unique identifier. This will usually be a username of some kind, but it can also be an email address, or any other unique identifier. The field must be unique (i.e., have unique=True set in its definition), unless you use a...
django.topics.auth.customizing#django.contrib.auth.models.CustomUser.USERNAME_FIELD
class models.CustomUserManager create_user(username_field, password=None, **other_fields) The prototype of create_user() should accept the username field, plus all required fields as arguments. For example, if your user model uses email as the username field, and has date_of_birth as a required field, then create...
django.topics.auth.customizing#django.contrib.auth.models.CustomUserManager
create_superuser(username_field, password=None, **other_fields) The prototype of create_superuser() should accept the username field, plus all required fields as arguments. For example, if your user model uses email as the username field, and has date_of_birth as a required field, then create_superuser should be defi...
django.topics.auth.customizing#django.contrib.auth.models.CustomUserManager.create_superuser
create_user(username_field, password=None, **other_fields) The prototype of create_user() should accept the username field, plus all required fields as arguments. For example, if your user model uses email as the username field, and has date_of_birth as a required field, then create_user should be defined as: def cre...
django.topics.auth.customizing#django.contrib.auth.models.CustomUserManager.create_user
class models.Group
django.ref.contrib.auth#django.contrib.auth.models.Group
name Required. 150 characters or fewer. Any characters are permitted. Example: 'Awesome Users'.
django.ref.contrib.auth#django.contrib.auth.models.Group.name
permissions Many-to-many field to Permission: group.permissions.set([permission_list]) group.permissions.add(permission, permission, ...) group.permissions.remove(permission, permission, ...) group.permissions.clear()
django.ref.contrib.auth#django.contrib.auth.models.Group.permissions
class models.Permission
django.ref.contrib.auth#django.contrib.auth.models.Permission
codename Required. 100 characters or fewer. Example: 'can_vote'.
django.ref.contrib.auth#django.contrib.auth.models.Permission.codename
content_type Required. A reference to the django_content_type database table, which contains a record for each installed model.
django.ref.contrib.auth#django.contrib.auth.models.Permission.content_type
name Required. 255 characters or fewer. Example: 'Can vote'.
django.ref.contrib.auth#django.contrib.auth.models.Permission.name
class models.PermissionsMixin is_superuser Boolean. Designates that this user has all permissions without explicitly assigning them. get_user_permissions(obj=None) Returns a set of permission strings that the user has directly. If obj is passed in, only returns the user permissions for this specific object....
django.topics.auth.customizing#django.contrib.auth.models.PermissionsMixin
get_all_permissions(obj=None) Returns a set of permission strings that the user has, both through group and user permissions. If obj is passed in, only returns the permissions for this specific object.
django.topics.auth.customizing#django.contrib.auth.models.PermissionsMixin.get_all_permissions
get_group_permissions(obj=None) Returns a set of permission strings that the user has, through their groups. If obj is passed in, only returns the group permissions for this specific object.
django.topics.auth.customizing#django.contrib.auth.models.PermissionsMixin.get_group_permissions
get_user_permissions(obj=None) Returns a set of permission strings that the user has directly. If obj is passed in, only returns the user permissions for this specific object.
django.topics.auth.customizing#django.contrib.auth.models.PermissionsMixin.get_user_permissions
has_module_perms(package_name) Returns True if the user has any permissions in the given package (the Django app label). If User.is_active and is_superuser are both True, this method always returns True.
django.topics.auth.customizing#django.contrib.auth.models.PermissionsMixin.has_module_perms
has_perm(perm, obj=None) Returns True if the user has the specified permission, where perm is in the format "<app label>.<permission codename>" (see permissions). If User.is_active and is_superuser are both True, this method always returns True. If obj is passed in, this method won’t check for a permission for the mo...
django.topics.auth.customizing#django.contrib.auth.models.PermissionsMixin.has_perm
has_perms(perm_list, obj=None) Returns True if the user has each of the specified permissions, where each perm is in the format "<app label>.<permission codename>". If User.is_active and is_superuser are both True, this method always returns True. If obj is passed in, this method won’t check for permissions for the m...
django.topics.auth.customizing#django.contrib.auth.models.PermissionsMixin.has_perms
is_superuser Boolean. Designates that this user has all permissions without explicitly assigning them.
django.topics.auth.customizing#django.contrib.auth.models.PermissionsMixin.is_superuser
class models.User
django.ref.contrib.auth#django.contrib.auth.models.User
check_password(raw_password) Returns True if the given raw string is the correct password for the user. (This takes care of the password hashing in making the comparison.)
django.ref.contrib.auth#django.contrib.auth.models.User.check_password
date_joined A datetime designating when the account was created. Is set to the current date/time by default when the account is created.
django.ref.contrib.auth#django.contrib.auth.models.User.date_joined