Backend ๐Ÿ’ป/Django

[Django] UserCreationForm ์‚ดํŽด๋ณด๊ธฐ & ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ ๊ตฌํ˜„

minjiwoo 2023. 3. 25. 12:38
728x90

Django ์—์„œ ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด UserCreationForm ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

Django์˜ UserCreationForm ํด๋ž˜์Šค๋Š” django.contrib.auth.form ํŒจํ‚ค์ง€์— ์žˆ์œผ๋ฉฐ, ์ด๋ฅผ ์ƒ์†ํ•ด์„œ ์œ ์ € ์ƒ์„ฑ form ์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

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

    # ์žฅ๊ณ  ๋ชจ๋ธ ํผ์€ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋กœ Meta ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๋ฉฐ, Meta ํด๋ž˜์Šค์—๋Š” ๋ชจ๋ธํผ์ด ์‚ฌ์šฉํ•  ๋ชจ๋ธ, ํ•„๋“œ ์ž‘์„ฑ์ด ํ•„์š”
    class Meta:
        model = User
        fields = ("username", "email")

 

๊ทธ๋Ÿฌ๋‹ค๊ฐ€ UserCreationForm ํด๋ž˜์Šค ๋‚ด๋ถ€๊ฐ€ ๊ถ๊ธˆํ•ด์„œ ๊ณต์‹ Django docs๋ฅผ ์ฐพ์•„๋ณด์•˜๋‹ค 

https://docs.djangoproject.com/en/1.8/_modules/django/contrib/auth/forms/

 

Django

The web framework for perfectionists with deadlines.

docs.djangoproject.com

๋‚ด๊ฐ€ ๋ฐฑ์—”๋“œ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” Django ๋ฐ–์— ์•ˆ์จ๋ด์„œ ๋น„๊ต๋ฅผ ๋ชปํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์›๋ž˜ ์ด๋ ‡๊ฒŒ ๊ธฐ๋Šฅ ๊ตฌํ˜„์ด ๊ฐ„ํŽธํ•ด๋„ ๋˜๋Š”๊ฑธ๊นŒ..???? ใ…Žใ…Ž...

Django๋Š” ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์ด ๋งŽ์•„์„œ ๋น ๋ฅด๊ฒŒ ๊ฐœ๋ฐœํ• ๋•Œ ๋„์›€์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค 

Django๋Š” ์šฐ๋ฆฌ๊ฐ€ ์—ฌ๋Š ์›น์‚ฌ์ดํŠธ์—์„œ๋‚˜ ํ”ํžˆ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋˜ ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ์„ ์ด๋ ‡๊ฒŒ ์ œ๊ณตํ•ด์ฃผ๊ณ  ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๋Š” ์ด๊ฑธ ๋‚ ๋จน...

class UserCreationForm(forms.ModelForm):
    """
    A form that creates a user, with no privileges, from the given username and
    password.
    """
    error_messages = {
        'password_mismatch': _("The two password fields didn't match."),
    }
    password1 = forms.CharField(label=_("Password"),
        widget=forms.PasswordInput)
    password2 = forms.CharField(label=_("Password confirmation"),
        widget=forms.PasswordInput,
        help_text=_("Enter the same password as above, for verification."))
	
    # forms.ModelForm ์„ ์ƒ์† ๋ฐ›์œผ๋ฏ€๋กœ, Meta class๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค 
    class Meta:
        model = User
        fields = ("username",)

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        # error handling 
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError(
                self.error_messages['password_mismatch'],
                code='password_mismatch',
            )
        return password2

# ์ƒ์„ฑํ•œ user ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฉ”์„œ๋“œ 
    def save(self, commit=True):
    # user ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์•„์ง ๋‹ค ์•ˆ๋“ค์–ด ์™”์œผ๋ฏ€๋กœ commit = False 
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
        return user

password1 ์— ์‚ฌ์šฉ์ž ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๊ฒŒํ•˜๊ณ , password2๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  password1 != password2 ์ผ๋•Œ error message๋ฅผ ๋ณด๋‚ด๋Š” ๋ฐฉ์‹์œผ๋กœ ์—๋Ÿฌ ํ•ธ๋“ค๋ง์„ ํ•˜๊ณ  ์žˆ๋‹ค. 

save() ํ•จ์ˆ˜๋กœ ์ƒ์„ฑํ•œ ์‚ฌ์šฉ์ž ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•œ๋‹ค. ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ์‚ฌ์šฉ์ž ๊ฐ์ฒด๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ˜น์€ admin ํŽ˜์ด์ง€์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

728x90