바뀜
둘러보기로 가기
검색하러 가기
6번째 줄:
6번째 줄:
+
+
19번째 줄:
21번째 줄:
− +
+
+
+
+
+
+
+
+
+
+
+
− +
− +
− get_user_model() # 이 안에 유저모델이 담긴다.+
+
+
+
+
+
+
+
+
+
+
+
+
− +
− request.user로 사용자에 접근한다.+
+
43번째 줄:
69번째 줄:
− +
− +
− +
→뷰에서 user모델 부르기
== 뷰에서 user모델 부르기 ==
== 뷰에서 user모델 부르기 ==
뷰에서 유저모델을 불러오는 방법은 3가지가 있다.
뷰에서 유저모델을 불러오는 방법은 3가지가 있다.
view에 request가 포함된다면 request.user로 사용자에 접근한다.(사실, 모델을 편집하지 않는 한 아래처럼 모델을 임포트 해올 필요는 없다.)
{| class="wikitable"
{| class="wikitable"
!방법
!방법
|settings를 경유해 부르기
|settings를 경유해 부르기
|settings에 지정된 유저모델을 불러오는 것.
|settings에 지정된 유저모델을 불러오는 것.
|from customuser.settings import AUTH_USER_MODEL
문자열이 반환되어 쓰인다.
(권장되는 방법)
|<syntaxhighlight lang="python">
from config.settings import AUTH_USER_MODEL
class User(AbstractUser):
followers = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name='followings'
)
</syntaxhighlight>
|-
|-
|메서드 사용
|메서드 사용
|유저모델을 불러오는 메서드를 사용한다.
|유저모델을 불러오는 메서드를 사용한다.
(가장 권장되는 방법)
클래스가 반환되어 쓰인다.
|from django.contrib.auth import get_user_model
(권장되는 방법)
|<syntaxhighlight lang="python">
from django.shortcuts import render
from django.contrib.auth import get_user_model # 이 안에 유저모델이 담긴다.
def main_show(request):
user = get_user_model()
if user.is_social == True:
test = '소셜'
else:
test = '안소셜.'
context = {'test':test}
return render(request, 'main/main.html', context)
</syntaxhighlight>
|}
|}
==user==
== 탬플릿에서 user 사용 ==
기본적으로 request가 담겨서 가기 때문에 view에서 user모델에 대한 사용이 없더라도 <code><nowiki>{{user}}</nowiki></code>을 통해 바로 유저모델을 사용할 수 있다.
==user 안의 속성.==
장고에서 사용하는 user의 하위속성엔 무엇이 있을까?
장고에서 사용하는 user의 하위속성엔 무엇이 있을까?
{| class="wikitable"
{| class="wikitable"
if request.user.is_authenticated:
if request.user.is_authenticated:
|-
|-
|
|id
|
|유저의 pk가 담긴다.
|
|posting.like_users.filter(id=request.user.id)
|-
|-
|
|