[Django] 비 SPA 방식으로 장고 Forms/Views를 적극 활용한 인스타그램 St 만들기#8_유저 페이지 구현

아직·2022년 7월 14일
0
post-thumbnail

1)

def user_page(request, username):
    page_user = get_object_or_404(get_user_model(), username=username, is_active=True)
    return render(request, "instagram/user_page.html",{
        "page_user": page_user,
    })
+
<img src="{{ page_user.avatar_url }}" class="rounded-circle" />
                {{ page_user.username }}

user는 현재 로그인된 유저이고, 우리가 찾는 username을 page_user로 찾아줄 것이다. 여기서 get_user_model()이 내부적으로 settings.AUTH_USER_MODEL을 참조해서 username=username을 넘겨줄 수 있는 것으로 보인다.

is_active를 켤 경우, 접근이 허용된 유저가 아니면 404 에러가 발생한다.

2)

<div class="col-sm-4">

한 줄이 12 column이니까 sm-4로 설정 시 3개의 img가 표현된다.

3)


def user_page(request, username):
...
    post_list.count = post.list.count()

len(post_list)는 post_list 전체를 가져와서 메모리에 얹은 다음에 메모리 상의 리스트의 갯수를 반환하는 반면, post_list.count는 db에 count query를 던지게 된다.

len()은 "여기에 대해서 수행할래." 느낌이라 데이터를 '손에 쥐고 있어야 하는' 느낌인데 count()는 "쟤를 대상으로 수행해줘."라는 어느정도 거리감이 느껴진다.

0개의 댓글