카테고리 없음

개발 일지 43 - 작성 글 확인

sorecord 2024. 9. 26. 18:26

회원이 로그인 했을때 자신이 작성한 게시글만을 확인하는 공간이 따로 필요할 것 같아서 my_post.html을 생각했다.

 

코드의 흐름도를 아래 그림처럼 구상했다.

 

 

 

my_post.html에 index파일을 복사해서 붙여넣고 하나하나 수정을 했다.

 

view파일에는 

 

def my_post_view(request):
    print('mypostttt')
    user_id = request.session.get('username')
    user = User_information.objects.get(username=user_id)
    
    post_list = Post_information.objects.filter(author=user).annotate(like_count=Count('like_users')).order_by('-created_at')
    print(post_list,'mypost')
    category = request.GET.get('category', 'all')  # 선택된 카테고리 가져오기
    print(category)
    
    # 선택된 카테고리에 따라 필터링
    if category != 'all' and category != '':
        post_list = post_list.filter(category=category)
    paginator = Paginator(post_list, 10)  # 페이지당 20개의 게시물을 표시합니다
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    
    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 = {
        'page_obj': page_obj,
        'user_profile': user_profile,
    }
    print(context['page_obj'])
    print(request)
    return render(request,'book/my_post.html',context)

 

위와 같은 코드를 사용했다. 

 

post_list에 로그인한 회원의 게시글만을 넣어서 my_post에 보내주었다.

 

path('my_post/',views.my_post_view, name='my_post_view'),

 

위와 같이 url파일도 작성해주었다.