목차
1. FBV로 회원가입 구현하기
2. CBV로 회원가입 구현하기
2.0. 기초설정, models.py 만들기
2.1. urls.py 만들기
2.2. views.py 만들기
2.3. views.py에 필요한 forms.py 만들기
2.4. template 만들기
> python manage.py startapp accounts
urls.py, forms.py 생성
# config/settings.py
INSTALLED_APPS = [
...
'accounts',
...
]
# config/urls.py
urlpatterns = [
...
path('accounts/', include('accounts.urls')),
...
]
# accounts/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
pass
# config/settings.py
AUTH_USER_MODEL = 'accounts.User'
> python manage.py makemigrations
> python mange.py migrate
from django.urls import path
from .views import SignupView
app_name = 'accounts'
urlpatterns = [
path('signup', SignupView.as_view(), name='signup'),
]
from django.contrib.auth import get_user_model
from django.views.generic import CreateView
from accounts.forms import SignupForm
User = get_user_model()
class SignupView(CreateView):
model = User
form_class = SignupForm
template_name = 'accounts/signup.html'
success_url = '/'
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model
User = get_user_model()
class SignupForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = User # accounts앱에서 등록한 Model인 User로 변경(장고 기본인 auth.User 사용 X)
<!-- index.html -->
<a href="{% url 'accounts:signup' %}">회원가입</a>
<!-- signup.html -->
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit" value="회원가입"/>
</form>