OneToOne을 이용해 User 모델 확장 하려다 코딩이 꼬인거 같내요
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from users.models import Profile
class UserForm(UserCreationForm):
email = forms.EmailField(label="이메일")
class Meta:
model = User
fields = ("username", "email")
class ProfileForm(forms.ModelForm):
birth_date = forms.DateField(label="생일")
nickname = forms.CharField(label="닉네임", max_length=8)
class Meta:
model = Profile
fields = ("birth_date", "nickname")
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')
birth_date = models.DateField()
nickname = models.CharField(max_length=8)
@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()
from django.contrib.auth import views as auth_views
from django.urls import path
from .import views
app_name = 'users'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(
template_name='users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('signup/', views.signup, name='signup'),
]
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from users.forms import UserForm, ProfileForm
from django.contrib.auth.decorators import login_required
from django.db import transaction
@login_required
@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_form.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('list')
else:
user_form = UserForm()
profile_form = ProfileForm()
return render(request, 'users/signup.html',
{'user_form': user_form, 'profile_form': profile_form})
회원가입에서 다른 정보도 수집하려다 코딩이 꼬인건지 오류가 뜨내요 ㅠㅠ 그냥 AbstractUser와 같은 다른방법으로 확장하는게 나을까요?
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/accounts/login/?next=/users/signup/
Using the URLconf defined in shop1.urls, Django tried these URL patterns, in this order:
admin/
product/
users/
[name='list']
^media/(?P
The current path, accounts/login/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
오류내용입니다
123 님 626
M 2021년 3월 13일 6:45 오후
1개의 답변이 있습니다. 1 / 1 Page
회원가입할때는 로그인이 필요한 상태가 아니므로 먼저 signup 함수의 @login_required
애너테이션을 제거하셔야 할 것 같습니다.
박응용 님
2021년 3월 14일 12:47 오전