바뀜

둘러보기로 가기 검색하러 가기
1,840 바이트 추가됨 ,  2021년 11월 3일 (수) 17:07
편집 요약 없음
57번째 줄: 57번째 줄:  
===무언가 누락했을 때 발생시키기===
 
===무언가 누락했을 때 발생시키기===
 
모델, 폼에서의 제약을 걸어주면 누락될 때 자연스레 발생시켜준다.
 
모델, 폼에서의 제약을 걸어주면 누락될 때 자연스레 발생시켜준다.
 +
 +
= 모델폼이 아닌, 그냥 폼에서 모델 다루기..(아직 검증 안됨. 프로필 구현하다가 멈춰서...) =
 +
아래 코드로... profile을 만들고, user에 대한 것을 수정, 저장할 수 있을까???? 해봐야 함...
 +
 +
모델폼이 아닌, 그냥 폼으로 여러 처리를 하고 싶을 때.<syntaxhighlight lang="python">
 +
    '''계정 속성을 검증해서 이 사이트에서 회원가입한 계정의 경우에만 접속할 수 있게 하자. 앗, 그러면...sns계정 로그인 시엔 어찌하지;'''
 +
    if request.method == "POST":
 +
        from .models import Profile
 +
        form = Profile_create_form(request.POST)
 +
        if form.is_valid():
 +
            username = form.cleaned_data.get('identifier')      # 유저아이디를 입력받는다.
 +
            raw_password = form.cleaned_data.get('password')    # 패스워드를 입력받는다.
 +
            user = authenticate(username=username, password=raw_password)  # 인증 진행
 +
            if user:  # 유저모델이 반환된다면.. 프로필 모델에 저장한다.
 +
                profile = Profile()
 +
                profile.username = form.cleaned_data.get('username')
 +
                profile.nickname = form.cleaned_data.get('nickname')
 +
                profile.email = form.cleaned_data.get('email')
 +
                profile = profile.save()
 +
                request.user.profile = profile  # 해당 로그인한 유저의 프로필을 이 프로필로 채운다.
 +
                request.user.save()
 +
                return redirect('account:profile')
 +
            else:
 +
                form.add_error('identifier','비밀번호 혹은 아이디가 다릅니다.')
 +
    else:
 +
        form = Profile_create_form()
 +
    context = {'form': form,
 +
              }
 +
    return render(request, 'account/profile_create.html', context)
 +
</syntaxhighlight>

둘러보기 메뉴