장고

개발 일지 30 - 댓글 프로필 이미지 추가 ..2

sorecord 2024. 9. 13. 10:41

어제 생겼던 오류를 해결할 방법은 어렵지 않았다.

 

새로 만든 함수를 사용을 해주는 것이 아닌 기존에 사용하던 함수를 이용해주는 방법이었다.

 

 user_id = request.session.get('user_id')

    try:
        user_profile = UserProfile.objects.get(user_id=user_id)
    except UserProfile.DoesNotExist:
        user_profile = None  # 프로필이 없으면 None으로 설정
        
  context = {
        ......
        'user_profile':user_profile,
    }

 

그 후 post_detail에서 user_profile값을 comment_template로 보내주는 방법을 찾아봤더니 아주 단순한 방법이 있었다.

 

 {% include 'book/comment_template.html' with comment=comment user_profile=user_profile %}

 

위와 같이 값을 보내고 

 

<form method="post" action="{% url 'comment_profile_image' post.pk %}" enctype="multipart/form-data">
    <img 
        src="{% if user_profile.profile_image %} http://127.0.0.1:8000/book{{ user_profile.profile_image }}{% endif %}" 
        alt="Profile Picture" 
        style="cursor: pointer; width: 50px; height: 50px; border-radius: 50%;" 

    >
</form>

 

바로 comment_template에서 값을 받아 출력하니 이미지가 잘 출력이 되었다. 이제 사이즈하고 위치를 수정을 해줘야 한다.