Backend/Python
원본 글 보기

[Django] 'matching query does not exist' 에러 해결

[Django] 'matching query does not exist' 에러 해결

에러 상황 :

matching query does not exist

로그인을 시도할 때 아이디와 (username) 일치하는 게 없는 상황.

회원가입을 시도하지 않은 임의의 아무 username 을 갖다 붙이고 로그인을 하려고 하면 위의 화면이 나온다.

"BoardMember matching query does not exist."

장고에서는 회원가입을 하지 않은 게스트가 로그인을 하려고 하면 이런 창을 뱉는 것 같다.

이 DoesNotExists 라는 창을 예외처리 해 주자.

답변자도 404 페이지를 처리 하라고 하시는 듯

해결

member/forms.py 에서 username 과 일치하는게 없을 때의 예외처리.

로그인 시도 시, username 과 일치하는 경우에 대해서만 코드가 작성되어 있다.

즉, 로그인을 시도할 때 아이디와 (username) 일치하는 게 없을 때의 예외처리가 되어 있지 않은 상태.



if username and password:
try:
        member = BoardMember.objects.get(username=username)
    except BoardMember.DoesNotExist:
        self.add_error('username', '아이디가 없습니다!')
return
        # 예외처리를 하고 return 을 실행해서 바로 아래 코드를 실행하지 않고 빠져나오게 한다.
if not check_password(password, member.password):
        self.add_error('password', '비밀번호가 다릅니다!')
else:
        self.user_id = member.id