사용자가 로그인하고 있는지 확인하는 방법(user.is_interated를 올바르게 사용하는 방법)
이 사이트를 보고 있는데, 어떻게 해야 할지 잘 모르겠어요.현재 사이트 사용자가 로그인(인증 완료)하고 있는지 확인해야 합니다.
request.user.is_authenticated
사용자가 로그인하고 있는 것이 확인되어도 다음 항목만 반환됩니다.
>
위 URL의 첫 번째 섹션에서 다음과 같은 다른 요청을 수행할 수 있습니다.
request.user.is_active
정상적인 응답을 반환합니다.
장고 1.10+ 업데이트
is_authenticated이제 Django 1.10의 속성이 되었습니다.
if request.user.is_authenticated:
# do something if the user is authenticated
NB: 이 방법은 Django 2.0에서 제거되었습니다.
장고 1.9 이상용
is_authenticated함수입니다.이렇게 불러야 돼요
if request.user.is_authenticated():
# do something if the user is authenticated
Peter Rowell이 지적한 바와 같이, 당신을 혼란스럽게 하는 것은 기본 Django 템플릿 언어에서는 함수를 호출하기 위해 괄호를 붙이지 않는다는 것입니다.템플릿 코드에서 다음과 같은 것을 볼 수 있습니다.
{% if user.is_authenticated %}
그러나, 파이썬 코드에서, 그것은 실제로 method입니다.User학급.
장고 1.10+
메서드가 아닌 Atribute를 사용합니다.
if request.user.is_authenticated: # <- no parentheses any more!
# do something if the user is authenticated
같은 이름의 메서드의 사용은 Django 2.0에서는 권장되지 않으며, Django 문서에서는 더 이상 언급되지 않습니다.
Note that for Django 1.10 and 1.11, the value of the property is a
CallableBool and not a boolean, which can cause some strange bugs. For example, I had a view that returned JSON
return HttpResponse(json.dumps({
"is_authenticated": request.user.is_authenticated()
}), content_type='application/json')
부동산으로 업데이트 된 후request.user.is_authenticated예외를 던지고 있었다.TypeError: Object of type 'CallableBool' is not JSON serializable솔루션은 시리얼화 시 CallableBool 객체를 적절하게 처리할 수 있는 JsonResponse를 사용하는 것이었습니다.
return JsonResponse({
"is_authenticated": request.user.is_authenticated
})
다음 블록이 작동해야 합니다.
{% if user.is_authenticated %}
<p>Welcome {{ user.username }} !!!</p>
{% endif %}
보기:
{% if user.is_authenticated %}
<p>{{ user }}</p>
{% endif %}
컨트롤러 기능에 데코레이터 추가:
from django.contrib.auth.decorators import login_required
@login_required
def privateFunction(request):
템플릿에서 인증된 사용자를 확인하는 경우 다음 작업을 수행합니다.
{% if user.is_authenticated %}
<p>Authenticated user</p>
{% else %}
<!-- Do something which you want to do with unauthenticated user -->
{% endif %}
사용자가 views.py 파일에 로그인(인증된 사용자)하고 있는지 여부를 확인하려면 다음 예시와 같이 "is_interated" 메서드를 사용합니다.
def login(request):
if request.user.is_authenticated:
print('yes the user is logged-in')
else:
print('no the user is not logged-in')
사용자가 html 템플릿파일에 로그인(인증된 사용자)하고 있는지 여부를 확인하려면 , 다음의 예로서도 사용할 수 있습니다.
{% if user.is_authenticated %}
Welcome,{{request.user.first_name}}
{% endif %}
이것은 단지 예시일 뿐이며, 필요에 따라 변경해 주세요.
나는 이것이 당신에게 도움이 되기를 바랍니다.
Django 2.0+ 버전의 경우 다음을 사용합니다.
if request.auth:
# Only for authenticated users.
상세한 것에 대하여는, https://www.django-rest-framework.org/api-guide/requests/#auth 를 참조해 주세요.
request.user.is_interated()는 Django 2.0+ 버전에서 삭제되었습니다.
언급URL : https://stackoverflow.com/questions/3644902/how-to-check-if-a-user-is-logged-in-how-to-properly-use-user-is-authenticated
'programing' 카테고리의 다른 글
| Python에서 예외를 인쇄하려면 어떻게 해야 합니까? (0) | 2022.09.21 |
|---|---|
| strtotime()은 dd/mm/YYY 형식에서 작동하지 않습니다. (0) | 2022.09.21 |
| 다른 경우 트리거(사이) (0) | 2022.09.21 |
| set Timeout 콜백에 올바른 "이" 컨텍스트를 전달하시겠습니까? (0) | 2022.09.21 |
| PHP 시스템의 로컬 IP를 가져오는 방법 (0) | 2022.09.21 |