๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป Programming/programming

[Django] ์œ ์ €์‹œ์Šคํ…œ : ๋””์žฅ๊ณ  ํ”„๋กœ์ ํŠธ ์‹œ์ž‘์„ ์œ„ํ•œ pre setting ๋‹จ๊ณ„๋ณ„ ์ •๋ฆฌ

๐Ÿ” ๋””์žฅ๊ณ ์— ๋Œ€ํ•œ ๊ธฐ์ดˆ์ ์ธ ๋‚ด์šฉ์€ ๋‹ค๋ฅธ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ !(๊ธ€ ์ œ์ผ ํ•˜๋‹จ์— ์ •๋ฆฌํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค)

django.contrib.auth, django-allauth์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ์ถ”ํ›„ ํฌ์ŠคํŒ… ์˜ˆ์ • :)


1๏ธโƒฃ์‚ฌ์ „ ์ค€๋น„

1. ํด๋” ๋งŒ๋“ค๊ธฐ

  • mkdir {ํด๋” ์ด๋ฆ„}
  • example) mkdir django-coplate
  • ๋””์žฅ๊ณ  ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค ๊ณต๊ฐ„

2. ๊ฐ€์ƒํ™˜๊ฒฝ ๋งŒ๋“ค๊ธฐ

  • pyenv virtualenv {ํŒŒ์ด์ฌ ๋ฒ„์ „} {๊ฐ€์ƒํ™˜๊ฒฝ ์ด๋ฆ„}
  • example) pyenv virtualenv 3.9.10 django_coplate_env
  • ์ƒ์„ฑ ํ›„ ์„ค์น˜๋œ python ๋ฒ„์ „ ๋ฐ ํ™˜๊ฒฝ ํ™•์ธ : pyenv versions

3. ๊ฐ€์ƒํ™˜๊ฒฝ ์ง€์ •

  • 1์—์„œ ๋งŒ๋“ค์–ด์ค€ ํด๋”๋กœ ์ด๋™ ํ›„ ๋ช…๋ น์–ด ์ž…๋ ฅ
  • pyenv local {๊ฐ€์ƒํ™˜๊ฒฝ ์ด๋ฆ„}
  • pyenv local django_coplate_env

4. ๋””์žฅ๊ณ  ์„ค์น˜

  • pip install django==2.2

5. ํ”„๋กœ์ ํŠธ ์ •์˜

  • django-admin startproject {ํ”„๋กœ์ ํŠธ ์ด๋ฆ„}
  • django-admin startproject coplate_project

6. ์•ฑ ์ƒ์„ฑ

  • python manage.py startapp {์ƒ์„ฑํ•  App ์ด๋ฆ„}
  • python manage.py startapp coplate
  • ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ์˜ settings.py ํŒŒ์ผ์˜ INSTALLED_APPS ๋ฆฌ์ŠคํŠธ์— ์ƒ์„ฑํ•œ ์•ฑ ์ถ”๊ฐ€

2๏ธโƒฃ ์œ ์ € ๋ชจ๋ธ ์ •์˜ํ•˜๊ธฐ

1. ์•ฑ ๋””๋ ‰ํ† ๋ฆฌ์˜ models.py์— ์œ ์ € ๋ชจ๋ธ ์ •์˜

# models.py
from django.db import models
from django.contrib.auth.models import AbstractUser  # ์œ ์ € ๋ชจ๋ธ

# Create your models here.
class User(AbstractUser):
    pass

 

2. ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ์˜ settings.py ํŒŒ์ผ์— ์•„๋ž˜ ์ฝ”๋“œ ์ถ”๊ฐ€ : ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ ์œ ์ € ๋ชจ๋ธ๋กœ ํ•ด๋‹น ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ์•Œ๋ ค์ฃผ๋Š” ์˜๋ฏธ

## settings.py ํŒŒ์ผ ๋งจ ์•„๋ž˜์— ์ถ”๊ฐ€
# ์œ ์ € ๋ชจ๋ธ๋กœ ํ•ด๋‹น ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ 
# AUTH_USER_MODEL = '{์•ฑ ์ด๋ฆ„}.User'
AUTH_USER_MODEL = 'coplate.User'

 

3. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ›„ ์ €์žฅ(๋งค์šฐ ์ค‘์š”)

## ํ„ฐ๋ฏธ๋„ : ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ƒ์„ฑ
python manage.py makemigrations
python manage.py migrate

3๏ธโƒฃ ์œ ์ € ๋ชจ๋ธ admin ํŽ˜์ด์ง€ ๋“ฑ๋ก & superuser ์ƒ์„ฑ

1. ์œ ์ €๋ชจ๋ธ admin์— ๋“ฑ๋ก : ์œ ์ €๋ชจ๋ธ์˜ ํŠน์ด์‚ฌํ•ญ์œผ๋กœ, UserAdmin์„ ๊ฐ™์ด ์ธ์ž๋กœ ์ฃผ์–ด ๋“ฑ๋กํ•ด์•ผํ•จ

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User

# Register your models here.
# ๋ชจ๋ธ์„ ๊ฐ€์ ธ์™€์„œ ์ธ์ž๋กœ ๋„ฃ์–ด์ค€๋‹ค
admin.site.register(User, UserAdmin)

 

2. superuser(๊ด€๋ฆฌ์ž ๊ณ„์ •) ์ƒ์„ฑ

## ํ„ฐ๋ฏธ๋„
python manage.py createsuperuser

 

createsuperuser!


4๏ธโƒฃ allauth set up

์•„๋ž˜ ๋ ˆํผ๋Ÿฐ์Šค์—๋„ ๊ธฐ์ž…ํ•ด๋‘์—ˆ์ง€๋งŒ, django-allauthInstallation doumentation์—์„œ ๋งค์šฐ ์นœ์ ˆํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ๊ณ  ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ! 

 

1. ํ„ฐ๋ฏธ๋„์—์„œ ํŒจํ‚ค์ง€ ์„ค์น˜

## ํ„ฐ๋ฏธ๋„
pip install django-allauth

 

2. settings.py ํŒŒ์ผ ์ˆ˜์ •

๐Ÿ” INSTALLED_APPS ์ˆœ์„œ : ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ์•„๋ž˜์˜ ์ˆœ์„œ๊ฐ€ ๊ด€๋ก€์ด๋‹ค(convention)

  1. Local apps : ๋‚ด๊ฐ€ ์ƒ์„ฑํ•œ ์•ฑ
  2. Third party apps : ์™ธ๋ถ€์—์„œ ๋Œ์–ด์˜จ ์•ฑ
  3. Django apps : ๋””์žฅ๊ณ  ๋‚ด๋ถ€์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์•ฑ

๐Ÿ” 'django.contrib.sites' : ์–ด๋–ค ๊ธฐ๋Šฅ์„ ์—ฌ๋Ÿฌ ์›น์‚ฌ์ดํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•ด์ค€๋‹ค

  • ๋น„์Šทํ•œ ์ปจํ…์ธ ๋‚˜ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์›น์‚ฌ์ดํŠธ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ํ•„์š”ํ•  ๋•Œ ํ™œ์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ ํ•˜๋‚˜๋กœ ์—ฌ๋Ÿฌ ์›น์‚ฌ์ดํŠธ ์šด์˜ ๊ฐ€๋Šฅ
  • ์—ฌ๊ธฐ์„œ SITE_ID๊ฐ€ ๊ฐ๊ฐ์˜ ์›น์‚ฌ์ดํŠธ์˜ ์•„์ด๋””

๐Ÿ” EAMIL_BACKEND

  • allauth๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ด๋ฉ”์ผ ์ธ์ฆ์ด๋‚˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐพ๊ธฐ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋ ค๋ฉด ์ด๋ฉ”์ผ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์–ด์•ผํ•˜๋Š”๋ฐ
  • ์ด ๋•Œ, ์ด๋ฉ”์ผ์„ ์–ด๋–ป๊ฒŒ ๋ณด๋‚ผ์ง€ ์„ค์ •ํ•˜๋Š” ์ฝ”๋“œ์ž„

 

## settings.py
# 1)์œ ์ €์ธ์ฆ ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค ์ถ”๊ฐ€
AUTHENTICATION_BACKENDS = [
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',

    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
]


# 2)ํ•„์š”ํ•œ ์•ฑ ๋ณต์‚ฌํ•ด์„œ ์ถ”๊ฐ€
INSTALLED_APPS = [
	# 1. Local apps(๋‚ด๊ฐ€ ์ƒ์„ฑํ•œ ์•ฑ)
    '{app}'
    
    # 2. Third party apps(๋Œ์–ด์˜จ ์•ฑ)
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    
    # 3. Django apps(๋””์žฅ๊ณ  ๊ธฐ๋ณธ ์•ฑ)
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]


# 3) ์•„๋ž˜ ๋‚ด์šฉ ์ •์˜
SITE_ID = 1

# Email settings
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"


## ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ์˜ urls.py์— ์•„๋ž˜ ๋‚ด์šฉ ์ถ”๊ฐ€
path('', include('allauth.urls'))

5๏ธโƒฃ ํ™•์ธ

๋””์žฅ๊ณ  ๊ฐœ๋ฐœ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜๊ณ http://0.0.0.0:8000/login/, http://0.0.0.0:8000/admin/ ์œผ๋กœ ์ ‘์†ํ•˜๋ฉด ์ž˜ ์ ‘์†๋œ๋‹ค!

## ํ„ฐ๋ฏธ๋„
python manage.py runserver 0.0.0.0:8000

1. ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ : http://0.0.0.0:8000/login/

 

login ํŽ˜์ด์ง€

2. ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€ : http://0.0.0.0:8000/admin/

์•„๊นŒ ๋งŒ๋“  superuser ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธํ•˜๋ฉด ๋œ๋‹ค. GUI๋กœ ํŽธํ•˜๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค:)

admin ํŽ˜์ด์ง€(๋กœ๊ทธ์ธ ์ „)

 

admin ํŽ˜์ด์ง€(๋กœ๊ทธ์ธ ํ›„)


๐Ÿ™ Reference