0. 준비 작업
1) common app 생성 - 사용자 관리 / 공통요소 작업 위해
(noori) c:\projects\noori $ python manage.py startapp common
common 폴더 생성됨 : C:\projects\noori\common
2) works app 생성
C:\projects\noori\works
3) templates 폴더 생성
C:\projects\noori\templates
C:\projects\noori\templates\common
4) createsuperuser 생성
(noori) c:\projects\noori>python manage.py createsuperuser
1. settings.py 설정
대상 파일 : config / settings.py
AUTH_USER_MODEL = 'common.CustomUser'
auth.User -> <app명>.CustomUser
예) common.CustomUser
2. 모델 생성
기존 AbstractUser를 확장하는 CustomUser라고 하는 새 사용자 모델로 업데이트
# 권장은 AbstractBaseUser이나 복잡하여 미채택
대상 파일 : common / models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
# 권장 : AbstractBaseUser
class CustomUser(AbstractUser):
email = models.EmailField(max_length=254)
3. forms for UserCreationForm and UserChangeForm
대상 파일 :common / forms.py
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = CustomUser
fields = UserCreationForm.Meta.fields + ('email',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
4. admin.py
대상 파일 : common / admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['username', 'email', 'is_staff', ] # admin창 표시 내역 지정
admin.site.register(CustomUser, CustomUserAdmin)
5. authentication 관련 설정
1) registration 디렉터리 생성 : project/templates/registration
2) settings.py 설정 - templates경로 설정 및 Login, Logout redirect url 설정
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
}
]
LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'
3) 템플렛 파일 생성
C:\projects\noori\templates\common\base.html
<!-- templates/common/base.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}Work App{% endblock title %}</title>
</head>
<body>
<main>
{% block content %}
{% endblock content %}
</main>
</body>
</html>
C:\projects\noori\templates\common\home.html
<!-- templates/common/home.html -->
{% extends 'common/base.html' %}
{% block title %}Home{% endblock title %}
{% block content %}
{% if user.is_authenticated %}
Hi!, {{ user.username }}
<p><a href="{% url 'logout' %}">Log Out</a></p>
{% else %}
<p>You are not logged in</p>
<a href="{% url 'login' %}">Log In</a> |
<a href="{% url 'signup' %}">Sign Up</a>
{% endif %}
{% endblock content %}
C:\projects\noori\templates\registration\login.html
<!-- templates/registration/login.html -->
{% extends 'common/base.html' %}
{% block title %}Log In{% endblock title %}
{% block content %}
<h2>Log In</h2>
<form method="post">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Log In</button>
</form>
{% endblock content %}
C:\projects\noori\templates\registration\signup.html
<!-- templates/registration/signup.html -->
{% extends 'common/base.html' %}
{% block title %}Sign Up{% endblock title %}
{% block content %}
<h2>Sign Up</h2>
<form method="post">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign Up</button>
</form>
{% endblock content %}
6. urls 설정
C:\projects\noori\config\urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('works.urls')),
path('', include('common.urls')),
]
C:\projects\noori\common\urls.py
from django.contrib import admin
from django.urls import path, include
from .views import HomePageView, SignUpView
urlpatterns = [
path('', HomePageView.as_view(), name="home"),
path('', include('django.contrib.auth.urls')),
path('signup/', SignUpView.as_view(), name='signup'),
]
7. views 설정
C:\projects\noori\common\views.py
from django.http import HttpResponse
from django.views.generic import TemplateView, CreateView
from django.urls import reverse_lazy
from .forms import CustomUserCreationForm
class HomePageView(TemplateView):
template_name = 'common/home.html'
class SignUpView(CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'registration/signup.html'
8. makemigrations, migrate 실행
(noori) c:\projects\noori>python manage.py makemigrations
(noori) c:\projects\noori>python manage.py migrate
'Django' 카테고리의 다른 글
generic views (0) | 2023.05.06 |
---|---|
함수 views (0) | 2023.05.06 |
style 적용 (0) | 2023.05.04 |
Django 가상환경 구축 (0) | 2023.04.12 |
댓글