장고 회원가입 모델 확장 시 오류
common/models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    rank = models.IntegerField()
    sq = models.IntegerField()
    name = models.CharField(max_length=20)

    def __str__(self):
        return self.name


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()
common/forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm, PasswordChangeForm
from django.contrib.auth.models import User
from common.models import Profile


class UserForm(UserCreationForm):
    email = forms.EmailField(label="이메일")

    class Meta:
        model = User
        fields = ("username", "email")


class ProfileForm(forms.ModelForm):
    rank = forms.IntegerField(label="기수")
    sq = forms.IntegerField(label="중대")
    name = forms.CharField(label="이름", max_length=20)

    class Meta:
        model = Profile
        fields = ("rank", "sq", "name")

common/views.py

from django.contrib import messages
from django.contrib.auth import authenticate, login, update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from common.forms import UserForm, ProfileForm
from django.contrib.auth.decorators import login_required
from django.db import transaction

@transaction.atomic
def signup(request):
    if request.method == "POST":
        user_form = UserForm(request.POST)
        profile_form = ProfileForm(request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile = profile_form.save(commit=False)
            profile.user = request.user
            profile.save()
            username = user_form.cleaned_data.get('username')
            raw_password = user_form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('index')
    else:
        user_form = UserForm()
        profile_form = ProfileForm()
    return render(request, 'common/signup.html',
                  {'user_form': user_form, 'profile_form': profile_form})

@login_required
def change_password(request):
    if request.method == 'POST':
        password_change_form = PasswordChangeForm(request.user, request.POST)
        if password_change_form.is_valid():
            user = password_change_form.save()
            update_session_auth_hash(request, user)
            messages.success(request, "비밀번호를 성공적으로 변경하였습니다.")
            return redirect('change_password')
        else:
            messages.error(request, '다시 제대로 입력해주세요.')
    else:
        password_change_form = PasswordChangeForm(request.user)
    return render(request, 'common/change_password.html', {'password_change_form':password_change_form})
templates/common/signup.html

{% extends "base.html" %}
{% block content %}
<div class="container my-3">
    <div class="row my-3">
        <div class="col-4">
            <h4>회원가입</h4>
        </div>
        <div class="col-8 text-right">
            <span>또는 <a href="{% url 'common:login' %}">로그인 하세요.</a></span>
        </div>
    </div>
    <form method="post" class="post-form">
        {% csrf_token %}
        {% include "form_errors.html" %}
        <div class="form-group">
            <label for="username">교번</label>
            <input type="text" class="form-control" name="username" id="username"
                   value="{{ form.username.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <label for="rank">기수</label>
            <input type="number" class="form-control" name="rank" id="rank"
                   value="{{ form.rank.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <label for="sq">중대</label>
            <input type="number" class="form-control" name="sq" id="sq"
                   value="{{ form.sq.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <label for="name">이름</label>
            <input type="text" class="form-control" name="name" id="name"
                   value="{{ form.name.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <label for="password1">비밀번호</label>
            <input type="password" class="form-control" name="password1" id="password1"
                   value="{{ form.password1.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <label for="password2">비밀번호 확인</label>
            <input type="password" class="form-control" name="password2" id="password2"
                   value="{{ form.password2.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <label for="email">이메일</label>
            <input type="text" class="form-control" name="email" id="email"
                   value="{{ form.email.value|default_if_none:'' }}">
        </div>
        <button type="submit" class="btn btn-warning">생성하기</button>
    </form>
</div>
{% endblock %}

로그인은 정상적으로 오류도 뜨고 잘 작동하는데
회원가입은 one-to-one으로 모델 확장했더니 빈 칸이나 잘못 입력해도 오류도 안뜨고
제대로 입력해도 저장도 안 되고 그냥 signup 화면으로 redirect만 되네요
어디서 잘못된걸까요?ㅜ

miso 51

M 2021년 6월 5일 4:21 오후

콘솔에 표시되는 장고 개발서버 로그에도 아무 오류표시가 없나요? - 박응용님, 2021년 6월 5일 4:52 오후 추천 , 대댓글
@박응용님 네 아무 오류표시도 안 떠요ㅠ - miso님, 2021년 6월 5일 9:45 오후 추천 , 대댓글
목록으로