Django 인증 시스템을 사용¶
This document explains the usage of Django’s authentication system in its default configuration. This configuration has evolved to serve the most common project needs, handling a reasonably wide range of tasks, and has a careful implementation of passwords and permissions. For projects where authentication needs differ from the default, Django supports extensive extension and customization of authentication.
Django authentication provides both authentication and authorization together and is generally referred to as the authentication system, as these features are somewhat coupled.
사용자
객체¶
User
objects are the core of the
authentication system. They typically represent the people interacting with
your site and are used to enable things like restricting access, registering
user profiles, associating content with creators etc. Only one class of user
exists in Django’s authentication framework, i.e., 'superusers'
or admin 'staff'
users are just user objects with
special attributes set, not different classes of user objects.
디폴트 사용자의 주요 속성은 :
See the full API documentation
for
full reference, the documentation that follows is more task oriented.
사용자 생성하기¶
The most direct way to create users is to use the included
create_user()
helper function:
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user("john", "lennon@thebeatles.com", "johnpassword")
# At this point, user is a User object that has already been saved
# to the database. You can continue to change its attributes
# if you want to change other fields.
>>> user.last_name = "Lennon"
>>> user.save()
만약 Django admin 이 설치되어 있다면, :ref:` 사용자를 상호작용적으로 추가<auth-admin>` 할 수 있다.
Superusers 생성하기¶
Create superusers using the createsuperuser
command:
$ python manage.py createsuperuser --username=joe --email=joe@example.com
...\> py manage.py createsuperuser --username=joe --email=joe@example.com
당신은 프롬프트를 통해 패스워드 입력을 해야할 것 입니다. 패스워드를 입력한 뒤, 사용자가 바로 생성될 것입니다. 만약 --username
or --email
옵션들을 사용하지 않으면, 이들의 값을 위해 프롬프트로 물어볼 것입니다.
패스워드 변경하기¶
Django 는 사용자 모델에 원본 패스워드(일반 텍스트)를 저장하지 않고, 오직 해쉬만 저장합니다. (디테일하게 보기 비밀번호 조작하기 문서 ). 이로 인해, 사용자의 패스워드 속성을 직접적으로 조작하려 시도하지 마십시오. 이는 사용자 생성을 위해 헬퍼 함수를 사용하는 이유입니다.
사용자의 패스워드를 변경하기 위해, 여러 옵션이 있습니다.
manage.py changepassword *username*
offers a method
of changing a user’s password from the command line. It prompts you to
change the password of a given user which you must enter twice. If
they both match, the new password will be changed immediately. If you
do not supply a user, the command will attempt to change the password
whose username matches the current system user.
You can also change a password programmatically, using
set_password()
:
>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username="john")
>>> u.set_password("new password")
>>> u.save()
If you have the Django admin installed, you can also change user’s passwords on the authentication system’s admin pages.
Django also provides views and forms that may be used to allow users to change their own passwords.
Changing a user’s password will log out all their sessions. See 패스워드 변경시 세션 무효화 for details.
사용자 인증하기¶
- aauthenticate(request=None, **credentials)¶
Asynchronous version:
aauthenticate()
Use
authenticate()
to verify a set of credentials. It takes credentials as keyword arguments,username
andpassword
for the default case, checks them against each authentication backend, and returns aUser
object if the credentials are valid for a backend. If the credentials aren’t valid for any backend or if a backend raisesPermissionDenied
, it returnsNone
. For example:from django.contrib.auth import authenticate user = authenticate(username="john", password="secret") if user is not None: # A backend authenticated the credentials ... else: # No backend authenticated the credentials ...
request
is an optionalHttpRequest
which is passed on theauthenticate()
method of the authentication backends.참고
This is a low level way to authenticate a set of credentials; for example, it’s used by the
RemoteUserMiddleware
. Unless you are writing your own authentication system, you probably won’t use this. Rather if you’re looking for a way to login a user, use theLoginView
.Changed in Django 5.0:aauthenticate()
function was added.
웹 요청에서 인증¶
Django는 :doc:`sessions`와 미들웨어를 사용하여 인증 시스템을 :class:`request objects <django.http.HttpRequest>`에 연결한다.
These provide a request.user
attribute
and a request.auser
async method
on every request which represents the current user. If the current user has not
logged in, this attribute will be set to an instance
of AnonymousUser
, otherwise it will be an
instance of User
.
그것들을 다음과 같이 :attr:`~django.contrib.auth.models.User.is_authenticated`로 구분할 수 있다.
if request.user.is_authenticated:
# Do something for authenticated users.
...
else:
# Do something for anonymous users.
...
Or in an asynchronous view:
user = await request.auser()
if user.is_authenticated:
# Do something for authenticated users.
...
else:
# Do something for anonymous users.
...
The HttpRequest.auser()
method was added.
사용자가 로그인 하는법¶
현재 세션에 연결하려는 인증된 사용자가 있는 경우 - 이것은 login()
기능으로 수행된다.
- alogin(request, user, backend=None)¶
Asynchronous version:
alogin()
사용자를 로그인하려면 보기에서
HttpRequest()
객체와User
객체가 필요하다. :func:`~django.contrib.auth.login()`은 Django의 세션 프레임워크를 사용하여 세션에 사용자 ID를 저장한다.익명 세션 동안 설정된 모든 데이터는 사용자가 로그인한 후에도 세션에 유지된다.
이 예는
authenticate()
및login()
:을 모두 사용하는 방법을 보여준다.from django.contrib.auth import authenticate, login def my_view(request): username = request.POST["username"] password = request.POST["password"] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid login' error message. ...
Changed in Django 5.0:alogin()
function was added.
인증 백엔드 선택¶
사용자가 로그인하면 사용자의 세션에 사용자의 ID와 인증에 사용된 백엔드가 저장된다. 이렇게 하면 동일한 :ref:`authentication backend <authentication-backends>`가 향후 요청에서 사용자의 세부 정보를 가져올 수 있다. 세션에 저장할 인증 백엔드는 다음과 같이 선택된다.
제공되는 경우 선택적
backend
인수 값을 사용한다.존재하는 경우
user.backend
속성 값을 사용한다. 이렇게 하면authenticate()
,login()
: ,backend`()
속성을 설정한다하나뿐인 경우 :setting:`AUTHENTICATION_BACKENDS`에 ``backend``를 사용한다
그렇지 않으면 예외를 발생시킨다.
사례 1과 2의 경우 backend
인수 또는 user.backend
속성의 값은 실제 백엔드 클래스가 아니라 점으로 구분된 import 경로 문자열(예: :setting:`AUTHENTICATION_BACKENDS`에 있음)이어야 한다.
사용자가 로그아웃 하는법¶
- alogout(request)¶
Asynchronous version:
alogout()
django.contrib.auth.login()`을 통해 로그인한 사용자를 로그아웃하려면 보기 내에서 :func:`django.contrib.auth.logout()`을 사용한다. :class:`~django.http.HttpRequest()
객체를 사용하며 반환 값이 없다. 예시:from django.contrib.auth import logout def logout_view(request): logout(request) # Redirect to a success page.
:func:`~django.contrib.auth.logout()`은 사용자가 로그인하지 않은 경우 오류를 발생시키지 않는다.
:func:`~django.contrib.auth.logout()`을 호출하면 현재 요청에 대한 세션 데이터가 완전히 지워진다. 모든 기존 데이터가 제거된다. 이는 다른 사람이 동일한 웹 브라우저를 사용하여 로그인하고 이전 사용자의 세션 데이터에 액세스하는 것을 방지하기 위한 것이다. 사용자가 로그아웃한 직후에 사용할 수 있는 세션에 무엇이든 넣고 싶다면 :func:`django.contrib.auth.logout()`을 호출한 후에 수행하십시오.
Changed in Django 5.0:alogout()
function was added.
로그인한 사용자의 접근 제한하기¶
기본 방법¶
페이지에 대한 액세스를 제한하는 기본적인 방법은 :attr:`request.user.is_authenticated <django.contrib.auth.models.User.is_authenticated>`를 확인하고 로그인 페이지로 리디렉션하는 것이다.
from django.conf import settings
from django.shortcuts import redirect
def my_view(request):
if not request.user.is_authenticated:
return redirect(f"{settings.LOGIN_URL}?next={request.path}")
# ...
…또는 오류 메시지 표시한다.
from django.shortcuts import render
def my_view(request):
if not request.user.is_authenticated:
return render(request, "myapp/login_error.html")
# ...
login_required
데코레이터¶
- login_required(redirect_field_name='next', login_url=None)[소스]¶
바로 가기로 편리한
login_required()
decorator::를 사용할 수 있다.from django.contrib.auth.decorators import login_required @login_required def my_view(request): ...
:func:`~django.contrib.auth.decorators.login_required`는 다음을 수행한다.
사용자가 로그인하지 않은 경우 쿼리 문자열에 현재 절대 경로를 전달하여
settings.LOGIN_URL<LOGIN_URL> `로 리디렉션한다. 예: `
/accounts/login/?next=/polls/3/``.사용자가 로그인되어 있으면 정상적으로 뷰를 실행한다. 뷰 코드는 사용자가 로그인했다고 가정할 수 있다.
기본적으로 인증 성공 시 사용자가 리디렉션되어야 하는 경로는
"next"``라는 쿼리 문자열 매개변수에 저장된다. 이 매개변수에 다른 이름을 사용하고 싶다면 :func:`~django.contrib.auth.decorators.login_required`는 선택적 ``redirect_field_name
매개변수::를 사용한다.from django.contrib.auth.decorators import login_required @login_required(redirect_field_name="my_redirect_field") def my_view(request): ...
redirect_field_name``에 값을 제공하면 리디렉션 경로를 저장하는 템플릿 컨텍스트 변수가 ``"next"
(기본값) 이 아닌redirect_field_name
값을 키로 사용하기 때문에 로그인 템플릿도 커스터마이즈해야 할 가능성이 크다.login_required`는 선택적 ``login_url`()
매개변수도 사용한다. 예시:from django.contrib.auth.decorators import login_required @login_required(login_url="/accounts/login/") def my_view(request): ...
login_url
매개변수를 지정하지 않으면 :setting:`settings.LOGIN_URL <LOGIN_URL>`과 로그인 보기가 제대로 연결되어 있는지 확인해야 합니다. 예를 들어 기본값을 사용하여 URLconf에 다음 줄을 추가한다.from django.contrib.auth import views as auth_views path("accounts/login/", auth_views.LoginView.as_view()),
:setting:`settings.LOGIN_URL <LOGIN_URL>`은 뷰 함수 이름과 :ref:`named URL patterns <naming-url-patterns>`도 허용한다. 이렇게 하면 설정을 업데이트하지 않고도 URLconf 내에서 로그인 뷰를 자유롭게 다시 매핑할 수 있다.
참고
login_required
decorator는 사용자의 is_active
플래그를 확인하지 않지만 기본 :setting:`AUTHENTICATION_BACKENDS`는 비활성 사용자를 거부한다.
더 보기
Django의 관리자를 위한 커스텀 뷰를 작성하는 경우(또는 내장된 뷰에서 사용하는 것과 동일한 권한 부여 확인이 필요한 경우) django.contrib.admin.views.decorators.staff_member_required()
decorator가 ``login_required()``의 유용한 대안임을 찾을 수 있다.
Support for wrapping asynchronous view functions was added.
The LoginRequiredMixin
mixin¶
:doc:class-based views </topics/class-based-views/index>`를 사용할 때 ``LoginRequiredMixin``을 사용하여 ``login_required``와 동일한 동작을 얻을 수 있다. 이 믹스인은 상속 목록에서 가장 왼쪽에 있어야 한다.
- class LoginRequiredMixin[소스]¶
뷰가 이 믹스인을 사용하는 경우 인증되지 않은 사용자의 모든 요청은 로그인 페이지로 리디렉션되거나
raise_exception
매개변수에 따라 HTTP 403 Forbidden 오류가 표시된다.:class:`~django.contrib.auth.mixins.AccessMixin`의 매개변수를 설정하여 권한이 없는 사용자 처리를 커스터마이즈할 수 있다.
from django.contrib.auth.mixins import LoginRequiredMixin class MyView(LoginRequiredMixin, View): login_url = "/login/" redirect_field_name = "redirect_to"
참고
login_required
데코레이터와 마찬가지로 이 믹스인은 사용자의 is_active
플래그를 확인하지 않지만 기본 :setting:`AUTHENTICATION_BACKENDS`는 비활성 사용자를 거부한다.
The login_not_required
decorator¶
When LoginRequiredMiddleware
is
installed, all views require authentication by default. Some views, such as the
login view, may need to disable this behavior.
- login_not_required()[소스]¶
Allows unauthenticated requests to this view when
LoginRequiredMiddleware
is installed.
테스트를 통과한 로그인 사용자에 대한 액세스 제한¶
특정 권한이나 다른 테스트를 기반으로 액세스를 제한하려면 기본적으로 이전 섹션에서 설명한 것과 동일한 작업을 수행한다.
뷰에서 직접 :attr:`request.user <django.http.HttpRequest.user>`에 대한 테스트를 실행할 수 있다. 예를 들어, 이 뷰는 사용자가 원하는 도메인에 이메일이 있는지 확인하고 그렇지 않은 경우 로그인 페이지로 리디렉션한다.
from django.shortcuts import redirect
def my_view(request):
if not request.user.email.endswith("@example.com"):
return redirect("/login/?next=%s" % request.path)
# ...
- user_passes_test(test_func, login_url=None, redirect_field_name='next')[소스]¶
단축키로 콜러블이
False``를 반환할 때 리디렉션을 수행하는 편리한 ``user_passes_test
decorator를 사용할 수 있다.from django.contrib.auth.decorators import user_passes_test def email_check(user): return user.email.endswith("@example.com") @user_passes_test(email_check) def my_view(request): ...
User()
객체를 취하고 사용자가 페이지를 볼 수 있는 경우 ``True``를 반환하는 콜러블. :func:`~django.contrib.auth.decorators.user_passes_test`는 자동으로 :class:`~django.contrib.auth.models.User`가 익명이 아닌지 확인하지 않는다는 점에 유의해야 한다.:func:`~django.contrib.auth.decorators.user_passes_test`는 두 개의 선택적 인수를 사용한다.
login_url
테스트를 통과하지 못한 사용자가 리디렉션될 URL을 지정할 수 있다. 로그인 페이지일 수 있으며 지정하지 않으면 기본값은 :setting:`settings.LOGIN_URL <LOGIN_URL>`이다.
redirect_field_name
:func:`~django.contrib.auth.decorators.login_required`와 동일하다. ``None``으로 설정하면 URL에서 제거된다. 테스트를 통과하지 못한 사용자를 “다음 페이지”가 없는 비로그인 페이지로 리디렉션하는 경우 이 작업을 수행할 수 있다.
예시:
@user_passes_test(email_check, login_url="/login/") def my_view(request): ...
Changed in Django 5.1:Support for wrapping asynchronous view functions and using asynchronous test callables was added.
- class UserPassesTestMixin[소스]¶
:doc:`class-based views </topics/class-based-views/index>`를 사용하는 경우 ``UserPassesTestMixin``을 사용하여 이를 수행할 수 있다.
- test_func()[소스]¶
수행되는 테스트를 제공하려면 클래스의
test_func()
메서드를 재정의해야 한다. 또한 :class:`~django.contrib.auth.mixins.AccessMixin`의 매개변수를 설정하여 권한이 없는 사용자 처리를 커스터마이즈할 수 있다.from django.contrib.auth.mixins import UserPassesTestMixin class MyView(UserPassesTestMixin, View): def test_func(self): return self.request.user.email.endswith("@example.com")
- get_test_func()[소스]¶
또한
get_test_func()
메소드를 재정의하여 mixin이 검사를 위해 (test_func()
대신) 다른 이름의 함수를 사용하도록 할 수도 있다.
UserPassesTestMixin
스태킹(Stacking)``UserPassesTestMixin``이 구현되는 방식으로 인해 상속 목록에 쌓을 수 없다. 다음은 작동하지 않는다:
class TestMixin1(UserPassesTestMixin): def test_func(self): return self.request.user.email.endswith("@example.com") class TestMixin2(UserPassesTestMixin): def test_func(self): return self.request.user.username.startswith("django") class MyView(TestMixin1, TestMixin2, View): ...
``TestMixin1``이 ``super()``를 호출하고 해당 결과를 고려하면 ``TestMixin1``은 더 이상 독립 실행형으로 작동하지 않는다.
permission_required
decorator¶
- 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 @permission_required("polls.add_choice") def my_view(request): ...
has_perm()
메서드와 마찬가지로 권한 이름은"<app label>.<permission codename>"
형식을 취한다(즉,polls
애플리케이션의 모델에 대한 권한의 경우polls.add_choice
).decorator는 반복 가능한 권한을 가질 수도 있습니다. 이 경우 사용자는 뷰에 액세스하기 위해 모든 권한을 가지고 있어야 한다.
permission_required()`도 선택적 ``login_url`()
매개변수를 사용한다.from django.contrib.auth.decorators import permission_required @permission_required("polls.add_choice", login_url="/loginpage/") def my_view(request): ...
login_required()
데코레이터에서와 같이 ``login_url``의 기본값은 :setting:`settings.LOGIN_URL <LOGIN_URL>`이다.raise_exception
매개변수가 지정되면 데코레이터는 로그인 페이지로 리디렉션하는 대신 :exc:`~django.core.exceptions.PermissionDenied`를 발생시켜 :ref:`403(HTTP Forbidden) view<http_forbidden_view>’를 표시한다.``raise_exception``을 사용하고 사용자에게 먼저 로그인할 기회를 주고 싶다면
login_required()
데코레이터를 추가할 수 있다:from django.contrib.auth.decorators import login_required, permission_required @login_required @permission_required("polls.add_choice", raise_exception=True) def my_view(request): ...
이는 또한 :class:`.LoginView`의 ``redirect_authenticated_user=True``이고 로그인한 사용자에게 필요한 모든 권한이 없는 경우 리디렉션 루프를 방지한다.
Support for wrapping asynchronous view functions was added.
PermissionRequiredMixin
믹스인¶
:doc:`class-based views`에 권한 검사를 적용하려면 ``PermissionRequiredMixin``을 사용할 수 있다.
- class PermissionRequiredMixin[소스]¶
이 믹스인은
permission_required
decorator와 마찬가지로 뷰에 액세스하는 사용자에게 주어진 권한이 모두 있는지 여부를 확인하다.permission_required
매개변수를 사용하여 권한(또는 권한의 이터러블)을 지정해야 한다.from django.contrib.auth.mixins import PermissionRequiredMixin class MyView(PermissionRequiredMixin, View): permission_required = "polls.add_choice" # Or multiple of permissions: permission_required = ["polls.view_choice", "polls.change_choice"]
:class:`~django.contrib.auth.mixins.AccessMixin`의 매개변수 중 하나를 설정하여 권한이 없는 사용자의 처리를 커스터마이즈할 수 있다.
다음 메서드를 재정의할 수도 있다.
인증 뷰¶
Django provides several views that you can use for handling login, logout, and password management. These make use of the stock auth forms but you can pass in your own forms as well.
Django provides no default template for the authentication views. You should create your own templates for the views you want to use. The template context is documented in each view, see All authentication views.
Using the views¶
There are different methods to implement these views in your project. The
easiest way is to include the provided URLconf in django.contrib.auth.urls
in your own URLconf, for example:
urlpatterns = [
path("accounts/", include("django.contrib.auth.urls")),
]
This will include the following URL patterns:
accounts/login/ [name='login']
accounts/logout/ [name='logout']
accounts/password_change/ [name='password_change']
accounts/password_change/done/ [name='password_change_done']
accounts/password_reset/ [name='password_reset']
accounts/password_reset/done/ [name='password_reset_done']
accounts/reset/<uidb64>/<token>/ [name='password_reset_confirm']
accounts/reset/done/ [name='password_reset_complete']
The views provide a URL name for easier reference. See the URL documentation for details on using named URL patterns.
If you want more control over your URLs, you can reference a specific view in your URLconf:
from django.contrib.auth import views as auth_views
urlpatterns = [
path("change-password/", auth_views.PasswordChangeView.as_view()),
]
The views have optional arguments you can use to alter the behavior of the
view. For example, if you want to change the template name a view uses, you can
provide the template_name
argument. A way to do this is to provide keyword
arguments in the URLconf, these will be passed on to the view. For example:
urlpatterns = [
path(
"change-password/",
auth_views.PasswordChangeView.as_view(template_name="change-password.html"),
),
]
All views are class-based, which allows you to easily customize them by subclassing.
All authentication views¶
This is a list with all the views django.contrib.auth
provides. For
implementation details see Using the views.
- class LoginView[소스]¶
URL name:
login
See the URL documentation for details on using named URL patterns.
Methods and Attributes
- template_name¶
The name of a template to display for the view used to log the user in. Defaults to
registration/login.html
.
- next_page¶
The URL to redirect to after login. Defaults to
LOGIN_REDIRECT_URL
.
- redirect_field_name¶
The name of a
GET
field containing the URL to redirect to after login. Defaults tonext
. Overrides theget_default_redirect_url()
URL if the givenGET
parameter is passed.
- authentication_form¶
A callable (typically a form class) to use for authentication. Defaults to
AuthenticationForm
.
- extra_context¶
A dictionary of context data that will be added to the default context data passed to the template.
- redirect_authenticated_user¶
A boolean that controls whether or not authenticated users accessing the login page will be redirected as if they had just successfully logged in. Defaults to
False
.경고
If you enable
redirect_authenticated_user
, other websites will be able to determine if their visitors are authenticated on your site by requesting redirect URLs to image files on your website. To avoid this “social media fingerprinting” information leakage, host all images and your favicon on a separate domain.Enabling
redirect_authenticated_user
can also result in a redirect loop when using thepermission_required()
decorator unless theraise_exception
parameter is used.
- success_url_allowed_hosts¶
A
set
of hosts, in addition torequest.get_host()
, that are safe for redirecting after login. Defaults to an emptyset
.
- get_default_redirect_url()[소스]¶
Returns the URL to redirect to after login. The default implementation resolves and returns
next_page
if set, orLOGIN_REDIRECT_URL
otherwise.
Here’s what
LoginView
does:If called via
GET
, it displays a login form that POSTs to the same URL. More on this in a bit.If called via
POST
with user submitted credentials, it tries to log the user in. If login is successful, the view redirects to the URL specified innext
. Ifnext
isn’t provided, it redirects tosettings.LOGIN_REDIRECT_URL
(which defaults to/accounts/profile/
). If login isn’t successful, it redisplays the login form.
It’s your responsibility to provide the html for the login template , called
registration/login.html
by default. This template gets passed four template context variables:form
: AForm
object representing theAuthenticationForm
.next
: The URL to redirect to after successful login. This may contain a query string, too.site
: The currentSite
, according to theSITE_ID
setting. If you don’t have the site framework installed, this will be set to an instance ofRequestSite
, which derives the site name and domain from the currentHttpRequest
.site_name
: An alias forsite.name
. If you don’t have the site framework installed, this will be set to the value ofrequest.META['SERVER_NAME']
. For more on sites, see “sites” 프레임워크.
If you’d prefer not to call the template
registration/login.html
, you can pass thetemplate_name
parameter via the extra arguments to theas_view
method in your URLconf. For example, this URLconf line would usemyapp/login.html
instead:path("accounts/login/", auth_views.LoginView.as_view(template_name="myapp/login.html")),
You can also specify the name of the
GET
field which contains the URL to redirect to after login usingredirect_field_name
. By default, the field is callednext
.Here’s a sample
registration/login.html
template you can use as a starting point. It assumes you have abase.html
template that defines acontent
block:{% extends "base.html" %} {% block content %} {% if form.errors %} <p>Your username and password didn't match. Please try again.</p> {% endif %} {% if next %} {% if user.is_authenticated %} <p>Your account doesn't have access to this page. To proceed, please login with an account that has access.</p> {% else %} <p>Please login to see this page.</p> {% endif %} {% endif %} <form method="post" action="{% url 'login' %}"> {% csrf_token %} <table> <tr> <td>{{ form.username.label_tag }}</td> <td>{{ form.username }}</td> </tr> <tr> <td>{{ form.password.label_tag }}</td> <td>{{ form.password }}</td> </tr> </table> <input type="submit" value="login"> <input type="hidden" name="next" value="{{ next }}"> </form> {# Assumes you set up the password_reset view in your URLconf #} <p><a href="{% url 'password_reset' %}">Lost password?</a></p> {% endblock %}
If you have customized authentication (see Customizing Authentication) you can use a custom authentication form by setting the
authentication_form
attribute. This form must accept arequest
keyword argument in its__init__()
method and provide aget_user()
method which returns the authenticated user object (this method is only ever called after successful form validation).
- class LogoutView[소스]¶
Logs a user out on
POST
requests.URL name:
logout
Attributes:
- next_page¶
The URL to redirect to after logout. Defaults to
LOGOUT_REDIRECT_URL
.
- template_name¶
The full name of a template to display after logging the user out. Defaults to
registration/logged_out.html
.
- redirect_field_name¶
The name of a
GET
field containing the URL to redirect to after log out. Defaults to'next'
. Overrides thenext_page
URL if the givenGET
parameter is passed.
- extra_context¶
A dictionary of context data that will be added to the default context data passed to the template.
- success_url_allowed_hosts¶
A
set
of hosts, in addition torequest.get_host()
, that are safe for redirecting after logout. Defaults to an emptyset
.
Template context:
title
: The string “Logged out”, localized.site
: The currentSite
, according to theSITE_ID
setting. If you don’t have the site framework installed, this will be set to an instance ofRequestSite
, which derives the site name and domain from the currentHttpRequest
.site_name
: An alias forsite.name
. If you don’t have the site framework installed, this will be set to the value ofrequest.META['SERVER_NAME']
. For more on sites, see “sites” 프레임워크.
- logout_then_login(request, login_url=None)[소스]¶
Logs a user out on
POST
requests, then redirects to the login page.URL name: No default URL provided
Optional arguments:
login_url
: The URL of the login page to redirect to. Defaults tosettings.LOGIN_URL
if not supplied.
- class PasswordChangeView[소스]¶
URL name:
password_change
Allows a user to change their password.
Attributes:
- template_name¶
The full name of a template to use for displaying the password change form. Defaults to
registration/password_change_form.html
if not supplied.
- success_url¶
The URL to redirect to after a successful password change. Defaults to
'password_change_done'
.
- form_class¶
A custom “change password” form which must accept a
user
keyword argument. The form is responsible for actually changing the user’s password. Defaults toPasswordChangeForm
.
- extra_context¶
A dictionary of context data that will be added to the default context data passed to the template.
Template context:
form
: The password change form (seeform_class
above).
- class PasswordChangeDoneView[소스]¶
URL name:
password_change_done
The page shown after a user has changed their password.
Attributes:
- template_name¶
The full name of a template to use. Defaults to
registration/password_change_done.html
if not supplied.
- extra_context¶
A dictionary of context data that will be added to the default context data passed to the template.
- class PasswordResetView[소스]¶
URL name:
password_reset
Allows a user to reset their password by generating a one-time use link that can be used to reset the password, and sending that link to the user’s registered email address.
This view will send an email if the following conditions are met:
The email address provided exists in the system.
The requested user is active (
User.is_active
isTrue
).The requested user has a usable password. Users flagged with an unusable password (see
set_unusable_password()
) aren’t allowed to request a password reset to prevent misuse when using an external authentication source like LDAP.
If any of these conditions are not met, no email will be sent, but the user won’t receive any error message either. This prevents information leaking to potential attackers. If you want to provide an error message in this case, you can subclass
PasswordResetForm
and use theform_class
attribute.참고
Be aware that sending an email costs extra time, hence you may be vulnerable to an email address enumeration timing attack due to a difference between the duration of a reset request for an existing email address and the duration of a reset request for a nonexistent email address. To reduce the overhead, you can use a 3rd party package that allows to send emails asynchronously, e.g. django-mailer.
Attributes:
- template_name¶
The full name of a template to use for displaying the password reset form. Defaults to
registration/password_reset_form.html
if not supplied.
- form_class¶
Form that will be used to get the email of the user to reset the password for. Defaults to
PasswordResetForm
.
- email_template_name¶
The full name of a template to use for generating the email with the reset password link. Defaults to
registration/password_reset_email.html
if not supplied.
- subject_template_name¶
The full name of a template to use for the subject of the email with the reset password link. Defaults to
registration/password_reset_subject.txt
if not supplied.
- token_generator¶
Instance of the class to check the one time link. This will default to
default_token_generator
, it’s an instance ofdjango.contrib.auth.tokens.PasswordResetTokenGenerator
.
- success_url¶
The URL to redirect to after a successful password reset request. Defaults to
'password_reset_done'
.
- from_email¶
A valid email address. By default Django uses the
DEFAULT_FROM_EMAIL
.
- extra_context¶
A dictionary of context data that will be added to the default context data passed to the template.
- html_email_template_name¶
The full name of a template to use for generating a text/html multipart email with the password reset link. By default, HTML email is not sent.
- extra_email_context¶
A dictionary of context data that will be available in the email template. It can be used to override default template context values listed below e.g.
domain
.
Template context:
form
: The form (seeform_class
above) for resetting the user’s password.
Email template context:
email
: An alias foruser.email
user
: The currentUser
, according to theemail
form field. Only active users are able to reset their passwords (User.is_active is True
).site_name
: An alias forsite.name
. If you don’t have the site framework installed, this will be set to the value ofrequest.META['SERVER_NAME']
. For more on sites, see “sites” 프레임워크.domain
: An alias forsite.domain
. If you don’t have the site framework installed, this will be set to the value ofrequest.get_host()
.protocol
: http or httpsuid
: The user’s primary key encoded in base 64.token
: Token to check that the reset link is valid.
Sample
registration/password_reset_email.html
(email body template):Someone asked for password reset for email {{ email }}. Follow the link below: {{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
The same template context is used for subject template. Subject must be single line plain text string.
- class PasswordResetDoneView[소스]¶
URL name:
password_reset_done
The page shown after a user has been emailed a link to reset their password. This view is called by default if the
PasswordResetView
doesn’t have an explicitsuccess_url
URL set.참고
If the email address provided does not exist in the system, the user is inactive, or has an unusable password, the user will still be redirected to this view but no email will be sent.
Attributes:
- template_name¶
The full name of a template to use. Defaults to
registration/password_reset_done.html
if not supplied.
- extra_context¶
A dictionary of context data that will be added to the default context data passed to the template.
- class PasswordResetConfirmView[소스]¶
URL name:
password_reset_confirm
Presents a form for entering a new password.
Keyword arguments from the URL:
uidb64
: The user’s id encoded in base 64.token
: Token to check that the password is valid.
Attributes:
- template_name¶
The full name of a template to display the confirm password view. Default value is
registration/password_reset_confirm.html
.
- token_generator¶
Instance of the class to check the password. This will default to
default_token_generator
, it’s an instance ofdjango.contrib.auth.tokens.PasswordResetTokenGenerator
.
- post_reset_login¶
A boolean indicating if the user should be automatically authenticated after a successful password reset. Defaults to
False
.
- post_reset_login_backend¶
A dotted path to the authentication backend to use when authenticating a user if
post_reset_login
isTrue
. Required only if you have multipleAUTHENTICATION_BACKENDS
configured. Defaults toNone
.
- form_class¶
Form that will be used to set the password. Defaults to
SetPasswordForm
.
- success_url¶
URL to redirect after the password reset done. Defaults to
'password_reset_complete'
.
- extra_context¶
A dictionary of context data that will be added to the default context data passed to the template.
- reset_url_token¶
Token parameter displayed as a component of password reset URLs. Defaults to
'set-password'
.
Template context:
form
: The form (seeform_class
above) for setting the new user’s password.validlink
: Boolean, True if the link (combination ofuidb64
andtoken
) is valid or unused yet.
- class PasswordResetCompleteView[소스]¶
URL name:
password_reset_complete
Presents a view which informs the user that the password has been successfully changed.
Attributes:
- template_name¶
The full name of a template to display the view. Defaults to
registration/password_reset_complete.html
.
- extra_context¶
A dictionary of context data that will be added to the default context data passed to the template.
Helper functions¶
- redirect_to_login(next, login_url=None, redirect_field_name='next')[소스]¶
Redirects to the login page, and then back to another URL after a successful login.
Required arguments:
next
: The URL to redirect to after a successful login.
Optional arguments:
login_url
: The URL of the login page to redirect to. Defaults tosettings.LOGIN_URL
if not supplied.redirect_field_name
: The name of aGET
field containing the URL to redirect to after login. Overridesnext
if the givenGET
parameter is passed.
Built-in forms¶
If you don’t want to use the built-in views, but want the convenience of not
having to write forms for this functionality, the authentication system
provides several built-in forms located in django.contrib.auth.forms
:
참고
The built-in authentication forms make certain assumptions about the user model that they are working with. If you’re using a custom user model, it may be necessary to define your own forms for the authentication system. For more information, refer to the documentation about using the built-in authentication forms with custom user models.
- class AdminPasswordChangeForm[소스]¶
A form used in the admin interface to change a user’s password, including the ability to set an
unusable password
, which blocks the user from logging in with password-based authentication.Takes the
user
as the first positional argument.Changed in Django 5.1:Option to disable (or reenable) password-based authentication was added.
- class AdminUserCreationForm[소스]¶
- New in Django 5.1.1.
A form used in the admin interface to create a new user. Inherits from
UserCreationForm
.It includes an additional
usable_password
field, enabled by default. Ifusable_password
is enabled, it verifies thatpassword1
andpassword2
are non empty and match, validates the password usingvalidate_password()
, and sets the user’s password usingset_password()
. Ifusable_password
is disabled, no password validation is done, and password-based authentication is disabled for the user by callingset_unusable_password()
.
- 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 whoseis_active
flag is set toFalse
. You may override this behavior with a custom policy to determine which users can log in. Do this with a custom form that subclassesAuthenticationForm
and overrides theconfirm_login_allowed()
method. This method should raise aValidationError
if the given user may not log in.For example, to allow all users to log in regardless of “active” status:
from django.contrib.auth.forms import AuthenticationForm class AuthenticationFormWithInactiveUsersOkay(AuthenticationForm): def confirm_login_allowed(self, user): pass
(In this case, you’ll also need to use an authentication backend that allows inactive users, such as
AllowAllUsersModelBackend
.)Or to allow only some active users to log in:
class PickyAuthenticationForm(AuthenticationForm): def confirm_login_allowed(self, user): if not user.is_active: raise ValidationError( _("This account is inactive."), code="inactive", ) if user.username.startswith("b"): raise ValidationError( _("Sorry, accounts starting with 'b' aren't welcome here."), code="no_b_users", )
- class BaseUserCreationForm[소스]¶
A
ModelForm
for creating a new user. This is the recommended base class if you need to customize the user creation form.It has three fields:
username
(from the user model),password1
, andpassword2
. It verifies thatpassword1
andpassword2
match, validates the password usingvalidate_password()
, and sets the user’s password usingset_password()
.
- 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 the email is sent to the user. If you choose to override this method, be mindful of handling potential exceptions raised due to email sending failures.- 매개변수:
subject_template_name – the template for the subject.
email_template_name – the template for the email body.
context – context passed to the
subject_template
,email_template
, andhtml_email_template
(if it is notNone
).from_email – the sender’s email.
to_email – the email of the requester.
html_email_template_name – the template for the HTML body; defaults to
None
, in which case a plain text email is sent.
By default,
save()
populates thecontext
with the same variables thatPasswordResetView
passes to its email context.
- class SetPasswordForm[소스]¶
A form that lets a user change their password without entering the old password.
- class UserChangeForm[소스]¶
A form used in the admin interface to change a user’s information and permissions.
- class UserCreationForm[소스]¶
Inherits from
BaseUserCreationForm
. To help prevent confusion with similar usernames, the form doesn’t allow usernames that differ only in case.
Authentication data in templates¶
The currently logged-in user and their permissions are made available in the
template context when you use
RequestContext
.
Technicality
Technically, these variables are only made available in the template
context if you use RequestContext
and the
'django.contrib.auth.context_processors.auth'
context processor is
enabled. It is in the default generated settings file. For more, see the
RequestContext docs.
사용자¶
When rendering a template RequestContext
, the
currently logged-in user, either a User
instance or an AnonymousUser
instance, is
stored in the template variable {{ user }}
:
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
This template context variable is not available if a RequestContext
is not
being used.
권한¶
The currently logged-in user’s permissions are stored in the template variable
{{ perms }}
. This is an instance of
django.contrib.auth.context_processors.PermWrapper
, which is a
template-friendly proxy of permissions.
Evaluating a single-attribute lookup of {{ perms }}
as a boolean is a proxy
to User.has_module_perms()
. For example, to check if
the logged-in user has any permissions in the foo
app:
{% if perms.foo %}
Evaluating a two-level-attribute lookup as a boolean is a proxy to
User.has_perm()
. For example,
to check if the logged-in user has the permission foo.add_vote
:
{% if perms.foo.add_vote %}
Here’s a more complete example of checking permissions in a template:
{% if perms.foo %}
<p>You have permission to do something in the foo app.</p>
{% if perms.foo.add_vote %}
<p>You can vote!</p>
{% endif %}
{% if perms.foo.add_driving %}
<p>You can drive!</p>
{% endif %}
{% else %}
<p>You don't have permission to do anything in the foo app.</p>
{% endif %}
It is possible to also look permissions up by {% if in %}
statements.
For example:
{% if 'foo' in perms %}
{% if 'foo.add_vote' in perms %}
<p>In lookup works, too.</p>
{% endif %}
{% endif %}
Managing users in the admin¶
When you have both django.contrib.admin
and django.contrib.auth
installed, the admin provides a convenient way to view and manage users,
groups, and permissions. Users can be created and deleted like any Django
model. Groups can be created, and permissions can be assigned to users or
groups. A log of user edits to models made within the admin is also stored and
displayed.
사용자 생성하기¶
You should see a link to “Users” in the “Auth” section of the main admin index page. The “Add user” admin page is different than standard admin pages in that it requires you to choose a username and password before allowing you to edit the rest of the user’s fields. Alternatively, on this page, you can choose a username and disable password-based authentication for the user.
Also note: if you want a user account to be able to create users using the Django admin site, you’ll need to give them permission to add users and change users (i.e., the “Add user” and “Change user” permissions). If an account has permission to add users but not to change them, that account won’t be able to add users. Why? Because if you have permission to add users, you have the power to create superusers, which can then, in turn, change other users. So Django requires add and change permissions as a slight security measure.
Be thoughtful about how you allow users to manage permissions. If you give a non-superuser the ability to edit users, this is ultimately the same as giving them superuser status because they will be able to elevate permissions of users including themselves!
패스워드 변경하기¶
User passwords are not displayed in the admin (nor stored in the database), but the password storage details are displayed. Included in the display of this information is a link to a password change form that allows admins to change or unset user passwords.