(06.15) 수정됨.
일반적으로 사용자 계정을 생성한다고 한다면 회원가입 페이지를 만들고, 가입 서식을 입력하는 과정을 상상할 것이다.
장고 프로젝트에서는 이를 위해 별도의 view를 구현해야 하지만, 굳이 그러지 않아도 된다.
콘솔창을 통해 계정 생성이 가능하기 때문이다.
콘솔 실행하기
예제를 따라하기 위해서는 cmd 콘솔을 실행해야 한다.
콘솔 실행 방법을 모른다면 패키지 설치와 프로젝트 설치 및 실행 방법을 보고 오자.
관리자 계정 만들기
"python manage.py createsuperuser" 명령을 통해 콘솔에서 관리자 계정을 생성할 수 있다.
(django-sample) C:\WhiteSeolpyo\django-sample\django_sample>python manage.py createsuperuser
You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin.
Run 'python manage.py migrate' to apply them.
Username (leave blank to use 'WhiteSeolpyo'): admin
Email address: admin@aaa.com
Password:
Password (again):
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
일반 계정 만들기
관리자 계정은 createsuperuser 명령으로 생성할 수 있지만, 일반 계정을 연결된 명령어가 없다.
"python mange.py shell" 명령으로 django shell을 호출하여 User model을 import한 다음 User object를 생성해주어야 한다.
shell을 종료할 때는 "Ctrl" + "Z" 키를 입력하면 된다.
(django-sample) C:\WhiteSeolpyo\django-sample\django_sample>python manage.py shell
6 objects imported automatically (use -v 2 for details).
Python 3.12.8 (tags/v3.12.8:2dc476b, Dec 3 2024, 19:30:04) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user(username='tester', email='tester@test.com', password='tester')
>>> user
<User: tester>
>>> ^Z