signup with custom user - pai-plznw4me/django-initializer GitHub Wiki
django ์ค์น์ ๊ธฐ๋ณธ ์ฑ ๊ธฐ๋ฅ์ผ๋ก ์ ์ ๋ชจ๋ธ์ ์ ๊ณตํ๋ค.ํ์ง๋ง ์ ์ ์์ฑ ๊ธฐ๋ฅ์ ๊ด๋ฆฌ์ ์ฑ์์ ์ ๊ณต๋๋ค. ํด๋น ํ์ด์ง์์๋ django ๊ธฐ๋ณธ ์ ์ ๋ชจ๋ธ์ ํ์ฉํด ํ์ ๊ฐ์ ํ์ด์ง๋ฅผ ๋ง๋ค๊ณ ๊ธฐ๋ณธ ์ ์ ๋ชจ๋ธ์ ํ์ฅํ์ฌ ๊ธฐ์กด ์ ์ ๋ชจ๋ธ์ ์๋ก์ด ํ๋๋ฅผ ์ถ๊ฐ ํ ์ ์๋๋ก ํ๋ค.
-
Model
-Form
-View
-Template
ํจํด์ผ๋ก ๊ฐ๋ฐ ๋จ.
- User ๋ชจ๋ธ ์์๋ฐ์ ์๋ก์ด field ์ถ๊ฐํ๊ธฐ
from django.contrib.auth.models import AbstractUser
from django.db import models
# Create your models here.
class CustomUser(AbstractUser): # <-- ์์ ๋ถ๋ถ
phone_number = models.CharField(max_length=11) # ์ ํ๋ฒํธ
career = models.IntegerField() # ๊ฒฝ๋ ฅ
rank = models.CharField(max_length=10) # ์ง๊ธ
date_company_joined = models.DateField() # ์
์ฌ ๊ธฐ๊ฐ
gender = models.CharField(max_length=3) # ์ฑ๋ณ
id_number = models.CharField(max_length=13) # ์ฃผ๋ฏผ๋ฑ๋ก๋ฒํธ
department = models.CharField(max_length=13) # ๋ถ์
resume = models.FileField(blank=True, null=True) # ์ด๋ ฅ์
id_photo = models.ImageField(blank=True, null=True, upload_to='') # ์ฆ๋ช
์ฌ์ง
def __str__(self):
return self.username
AbstractUser
์๋์ ๊ฐ์ field์ ๊ฐ์ง๊ณ ์๋ค. username
, first_name
, last_name
, email
, date_joined
,is_staff
๊ทธ๋ฌ๋ฏ๋ก AbstractUser ์ ์์๋ฐ๋ CustomUser ์์๋ ์ AbstractUser ์์ ์ ์๋ field ์ด์ธ์ ์ถ๊ฐํ๊ณ ์ถ์ field ์ ์ ์ํ๋ฉด ๋๋ค
createsupseruser
์ผ๋ก ์ํผ์ ์ ์์ฑ์ admin site ์ ์ ๊ทผ์ด ์๋ ์ ์๋ค.
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = ('username',
'date_company_joined',
'phone_number',
'career',
'rank',
'date_company_joined',
'gender',
'id_number',
'department',
'resume',
'id_photo',
'password1',
'password2') # <- User ์์ฑ์ ๋ณด์ฌ์ค field ์ ์ ์ํ๋ค.
profile_fields = ('username',
'date_company_joined',
'phone_number',
'career',
'rank',
'date_company_joined',
'gender',
'id_number',
'department',
'resume',
'id_photo')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# null=True๋ก ์ค์ ํ ํ๋์ ๋ํด required๋ฅผ False๋ก ์ค์
self.fields['resume'].required = False
self.fields['id_photo'].required = False
UserCreationForm
์ ์์ ๊ด๊ณ๋ ์๋์ ๊ฐ๋ค. forms.ModelForm
โBaseUserCreationForm
โ UserCreationForm
ModelForm
์ด ๋ถ๋ชจ ํด๋์ค์ด๋ค. ModelForm
์ฌ์ฉ ๋ฐฉ๋ฒ๋๋ก UserCreateForm
์ ์ฌ์ฉํ๋ฉด ๋๋ค.
Step 3. Django ์์ ๊ธฐ์กด User ์ ๋ฐ๋ผ๋ณด์ง ์๊ณ CustomUser ์ ๊ธฐ๋ณธ User ๋ก ๋ฐ๋ผ๋ณด๊ฒ ํ๋ค.
# settings.py
...
AUTH_USER_MODEL = 'accounts.CustomUser'

- [reference](https://learndjango.com/tutorials/django-custom-user-model)
- [reference 1 ](https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html)
<!-- templates/registration/login.html -->
<h2>Signup</h2>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Signup</button>
</form>
{{ errors }}
def signup(request):
if request.method == 'POST':
form = CustomUserCreationForm(request.POST, request.FILES) # <-- FILES ์ ๊ฐ์ด ์
๋ ฅ์ด ๋์ด์ผ ํ๋ค.
if form.is_valid():
# file field ๋ฐ์ดํฐ ์ ์ฅ
inst = form.save(commit=False)
# file field ๋ฐ์ดํฐ ์ ์ฅ
id_photo = form.cleaned_data['id_photo']
inst.id_photo.save(id_photo.name, id_photo)
# ์ผ๋ฐ form data ์ ์ฅ
inst.save()
# login
auth.login(request, inst)
# profile ํ์ด์ง๋ก redirect ํจ
return redirect('/account/profile')
else:
print('Errors:' , form.errors)
return render(request, template_name='account/signup.html', context={'errors': form.errors})
elif request.method == 'GET':
form = CustomUserCreationForm() # <-๋ฐ๋ ๋ถ๋ถ
return render(request, template_name='account/signup.html', context={'form': form})
else:
raise NotImplementedError
{% load index %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for field in field_names %}
{% with field_type=types|index:forloop.counter0 attr=user|getattribute:field %}
{% if field_type == 'ImageField' %}
{{ attr|getattribute:'url' }}
<img src={{ attr|getattribute:'url' }}>
{% else %}
<p>{{ field }} : {{ attr }} : {{ types | index:forloop.counter0 }}</p>
{% endif %}
{% endwith %}
{% endfor %}
</body>
</html>
@csrf_exempt
def profile(request):
user = CustomUser.objects.get(username=request.user)
# field ์ด๋ฆ์ ์ถ์ถํฉ๋๋ค.
field_names = CustomUserCreationForm.Meta.profile_fields
# field ์ type ์ ์ถ์ถํฉ๋๋ค.
types = []
for name in field_names:
field = user._meta.get_field(name)
# field ๊ฐ Image type ์ด๋ฉด 'ImageType' ์ผ๋ก ํ์
์ ์ ์ฅํฉ๋๋ค.
# ๋ชจ๋ธ ํ๋์ ๋ด๋ถ ํ์
์ ์ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฌ์ฉ๋๋ ํ์
์
๋๋ค.
# get_internal_type()์ผ๋ก ๋ฐํ๋๋ ๊ฐ์ FileField ์
๋๋ค.
if isinstance(field, models.ImageField):
types.append('ImageField')
else:
types.append(field.get_internal_type())
# template ์ rendering ํฉ๋๋ค.
context = {'user': user, 'field_names': field_names, 'types':types}
return render(request, template_name='account/profile.html', context=context)