바뀜

둘러보기로 가기 검색하러 가기
1,515 바이트 추가됨 ,  2021년 11월 4일 (목) 14:55
59번째 줄: 59번째 줄:  
=== view 추가 ===
 
=== view 추가 ===
 
다음과 같이 뷰를 추가한다.<syntaxhighlight lang="python">
 
다음과 같이 뷰를 추가한다.<syntaxhighlight lang="python">
 +
from .models import Profile
 +
 
@login_required()
 
@login_required()
 
def profile_create(request):
 
def profile_create(request):
     pass
+
     if request.method == "POST":
 +
        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>
 
</syntaxhighlight>
    
=== view에서 사용할 form 추가 ===
 
=== view에서 사용할 form 추가 ===
<syntaxhighlight lang="python">
+
계정 검증을 해야하기 때문에 모델에 기반한 폼이 아니라 일반 폼으로 제작한다.<syntaxhighlight lang="python">
from .models import Profile
+
class Profile_create_form(forms.Form):
class ProfileForm(forms.ModelForm):
+
     identifier = forms.CharField()
     class Meta:
+
    password = forms.PasswordInput()
        model = Profile
+
    username = forms.CharField(max_length=10)
        fields = ['username', 'email', 'nickname']
+
    nickname = forms.CharField(max_length=10)
 +
    email = forms.EmailField()
 
</syntaxhighlight>
 
</syntaxhighlight>
 
[[분류:장고 기능구현(초급)]]
 
[[분류:장고 기능구현(초급)]]

둘러보기 메뉴