์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

์žฅ๊ณ  (Django) - ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ (MVC)

  • -

 

*๋น„๋ฐ€๋ฒˆํ˜ธ๋ž€๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ ๋ž€์„ ๋‹ค๋ฅด๊ฒŒ ์ž…๋ ฅํ–ˆ์„๋•Œ

 

*์•„๋ฌด๊ฒƒ๋„ ์ž…๋ ฅํ•˜์ง€ ์•Š๊ณ  ๋“ฑ๋ก ํ–ˆ์„๋•Œ

 

*Admin์—์„œ ๋“ฑ๋ก๋œ ๋ชฉ๋ก

 

 

 

 

 

์ด์ „ ํฌ์ŠคํŒ…์—์„œ ์ƒ์„ฑํ–ˆ๋˜ user๋ผ๋Š” app์— ํšŒ์›๊ฐ€์ž…์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์ „์†ก ๋ฐ ์‚ฝ์ž… ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

1. ์žฅ๊ณ  ORM์„ ์‚ฌ์šฉํ•˜์—ฌ DB ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ

ํšŒ์›๊ฐ€์ž… ์–‘์‹์— ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’(name, password)๋ฅผ DB์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ models.py ์ž…๋‹ˆ๋‹ค.

 

*userํด๋” - models.py

from django.db import models

# Create your models here.

class User(models.Model): #์žฅ๊ณ ์—์„œ ์ œ๊ณตํ•˜๋Š” models.Model๋ฅผ ์ƒ์†๋ฐ›์•„์•ผํ•œ๋‹ค.
    username = models.CharField(max_length=64,verbose_name = '์‚ฌ์šฉ์ž๋ช…')
    password = models.CharField(max_length=64,verbose_name = '๋น„๋ฐ€๋ฒˆํ˜ธ')
    registered_dttm = models.DateTimeField(auto_now_add=True,verbose_name='๋“ฑ๋ก์‹œ๊ฐ„') 
    #์ €์žฅ๋˜๋Š” ์‹œ์ ์˜ ์‹œ๊ฐ„์„ ์ž๋™์œผ๋กœ ์‚ฝ์ž…ํ•ด์ค€๋‹ค.


    class Meta: #๋ฉ”ํƒ€ ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ”๋ช… ์ง€์ •
        db_table = 'test_user'

ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , models.Model์„ ์ƒ์†๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. (์žฅ๊ณ  ORM์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.)

ORM : SQL์ฟผ๋ฆฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์žฅ๊ณ  ๋ชจ๋ธ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ

models.CharField : ๋ฌธ์ž๋ฅผ ๋„ฃ์„ ํ•„๋“œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. max_length ์˜ต์…˜์œผ๋กœ ๊ธธ์ด๋ฅผ ์ œํ•œํ•ฉ๋‹ˆ๋‹ค. (์ œํ•œ๊ธธ์ด ์—†์ด ๊ธด ๋ฌธ์ž์—ด ex)๊ฒŒ์‹œ๊ธ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” models.TextField ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.)  

models.DateTimeField - auto_now_add : ํ˜„์žฌ์‹œ๊ฐ„ ์‚ฝ์ž… ์˜ต์…˜ (USE๋ผ๋Š” ํ‘œ์ค€์‹œ๊ฐ„์ด ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์‹œ๊ฐ„์„ ์‚ฝ์ž…ํ•˜๋ ค๋ฉด Time_zone์„ ๋ฐ”๊พธ๋ฉด ๋ฉ๋‹ˆ๋‹ค )

verbose_name : ๋‚˜์ค‘์— admin(๊ด€๋ฆฌ์ž)ํŽ˜์ด์ง€์—์„œ ๋ณด์—ฌ์ง€๋Š” ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. (์•Œ์•„๋ณด๊ธฐ ์‰ฌ์šด ์„ค๋ช…์„ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค.)

Meta (class): ์ƒ์„ฑํ•œ ๋ชจ๋ธํด๋ž˜์Šค์— ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์„ค์ •ํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ๋Š” ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

Meta (class) - db_table  : ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์„ค์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ž๋™์œผ๋กœ ํด๋ž˜์Šค๋ช… + app์ด๋ฆ„ ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ด๋ฆ„์ด ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

 

 

 

Time_zone ์…‹ํŒ… (Asia/seoul ์‹œ๊ฐ„)

*setting.py

TIME_ZONE =  'Asia/Seoul'   # timezone1์„œ์šธ
USE_I18N = True
USE_L10N = True
USE_TZ = False

 

 

 

 


 

 

2. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์ƒ์„ฑ (make migrations)

ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅ
python manage.py makemigrations 

ํ„ฐ๋ฏธ๋„์ฐฝ์— python manage.py makemigrations ์„ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค. ์ž…๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด, userํด๋” ํ•˜์œ„์— migrations ๋ผ๋Š” ํด๋”๊ฐ€ ์ƒ์„ฑ๋˜๋ฉฐ, 0001_initial.py๋ผ๋Š” ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์ „ ,์ดˆ์•ˆ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

(์ด๋•Œ 0001์˜ ์ˆซ์ž๋Š” ์ˆœ๋ฒˆ์ด๋ฉฐ, ์ถ”ํ›„ ํŒŒ์ผ์ˆ˜์ •ํ›„ ๋‹ค์‹œ ๋ชจ๋ธ์„ ์žฌ์ƒ์„ฑํ•˜๋ฉด 0002๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. )

์ •์ƒ์ ์œผ๋กœ ์ž…๋ ฅํ–ˆ๋‹ค๋ฉด ๋‹ค์Œ ๋ฉ”์‹œ์ง€์ฐฝ์ด ๋œน๋‹ˆ๋‹ค.

 

 

3.๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ DB์— ๋ฐ˜์˜ํ•˜๊ธฐ (migrate)

ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅ
python manage.py migrate

์•„๊นŒ ๋งŒ๋“ค์—ˆ๋˜ ํด๋ž˜์Šค์˜ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์˜ต์…˜๋“ค์„ ์ ์šฉํ•ด์ค๋‹ˆ๋‹ค. (๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.) 

ํ”„๋กœ์ ํŠธ์˜ setting.py๋ฅผ ๋ณด์‹œ๋ฉด DATABASE๋Š” sqlite3์œผ๋กœ ์„ค์ •๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.(์ดˆ๊ธฐ์„ค์ •)

๋”ฐ๋ผ์„œ ์œ„์˜ migrate๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ๋˜๋ฉด sqlite3์œผ๋กœ ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

์ฆ‰, DB๊ด€๋ จํ•˜์—ฌ ์ˆ˜์ •์‚ฌํ•ญ์ด ์žˆ์œผ๋ฉด ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ๋‹ค์‹œ ํ•ด์ฃผ์–ด์•ผ๋ฉ๋‹ˆ๋‹ค.


 

4. admin ์— ๋ชจ๋ธ Class ๋“ฑ๋กํ•˜๊ธฐ

๋ชจ๋ธ๋„ ์ƒ์„ฑํ–ˆ๊ณ , migrate๋„ ๋๋ƒˆ์Šต๋‹ˆ๋‹ค. admin.py๋ฅผ ์—ด์–ด์ฃผ๊ณ  ๋“ฑ๋ก(regist)์‹œ์ผœ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

admin์€ ์žฅ๊ณ ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. ์ด๊ณณ์—์„œ ์‚ญ์ œ, ์ถ”๊ฐ€ ๋“ฑ์˜ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ modelํด๋ž˜์Šค๋ฅผ ๋“ฑ๋กํ•˜์—ฌ admin์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.

from django.contrib import admin
from .models import User   #๊ฐ™์€ ๊ฒฝ๋กœ์˜ models.py์—์„œ User๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ž„ํฌํŠธํ•œ๋‹ค.

# Register your models here.

class UserAdmin(admin.ModelAdmin) :
    list_display = ('username', 'password')


admin.site.register(User, UserAdmin) #site์— ๋“ฑ๋ก

admin.ModelAdmin :์„ ์ƒ์†๋ฐ›๊ฒŒ ๋˜๋ฉด, adminํŽ˜์ด์ง€์—์„œ ์–ด๋– ํ•œ Column์„ ๊ด€๋ฆฌํ• ์ง€ ๋“ฑ์— ๋Œ€ํ•œ ์„ค์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

list_display : ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์—์„œ ๋ณผ Column์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

 

๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์Šˆํผ๊ณ„์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. 

์Šˆํผ๊ณ„์ • ์ƒ์„ฑ
ํ„ฐ๋ฏธ๋„ ์ž…๋ ฅ
python manage.py createsuperuser

์œ„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์Šˆํผ๊ณ„์ •์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„œ๋ฒ„ ์‹คํ–‰ํ›„ adminํŽ˜์ด์ง€๋ฅผ ํ™•์ธํ• ๋•Œ ํ•„์š”ํ•˜๋‹ˆ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

 


 

5. ์„œ๋ฒ„ ์‹คํ–‰ํ•ด๋ณด๊ธฐ

ํ„ฐ๋ฏธ๋„์ž…๋ ฅ
python manage.py runserver

์œ„ ๋ช…๋ น์–ด๋กœ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰์‹œํ‚จ ๋’ค http://127.0.0.1:8000/admin ํŽ˜์ด์ง€๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. 

๋“ค์–ด๊ฐ€๋ฉด ๋กœ๊ทธ์ธ์„ ํ•˜๋ผ๊ณ  ๋‚˜์˜ฌํ…๋ฐ, ๋ฐฉ๊ธˆ๋งŒ๋“  ์Šˆํผ๊ณ„์ •์„ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

๋‘๋ฒˆ์งธ ์ค„์˜ USER (App)๋ผ๋Š” ์•ฑ์ด ์ƒ์„ฑ๋˜์—ˆ๊ณ , Users๋ผ๋Š” ๋ชจ๋ธ์ด ์ƒ์„ฑ๋จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ž๋™์œผ๋กœ ๋ณต์ˆ˜ํ˜•์ด ์ ์šฉ๋˜์–ด Users๊ฐ€ ๋˜๋Š”๋ฐ์š”, ์ด ๋ถ€๋ถ„์€ models.py์—์„œ ์ˆ˜์ • ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

(verbose_name_plural = "Users ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ด๋ฆ„")

 

 

๊ทธ๋Ÿผ Users๋ฅผ ํ•œ๋ฒˆ ํด๋ฆญํ•ด ๋ด…๋‹ˆ๋‹ค.

ํด๋ฆญํ•˜๋ฉด user object๋ผ๋Š”๊ฒŒ ๋‚˜์˜ต๋‹ˆ๋‹ค. ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด username์ด ์ถœ๋ ฅ๋˜๋„๋ก ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.

 

Admin ํŽ˜์ด์ง€

*userํด๋” - models.py

from django.db import models

# Create your models here.

class User(models.Model): #์žฅ๊ณ ์—์„œ ์ œ๊ณตํ•˜๋Š” models.Model๋ฅผ ์ƒ์†๋ฐ›์•„์•ผํ•œ๋‹ค.
    username = models.CharField(max_length=64,verbose_name = '์‚ฌ์šฉ์ž๋ช…')
    password = models.CharField(max_length=64,verbose_name = '๋น„๋ฐ€๋ฒˆํ˜ธ')
    registered_dttm = models.DateTimeField(auto_now_add=True,verbose_name='๋“ฑ๋ก์‹œ๊ฐ„')
    
    def __str__(self): # ์ด ํ•จ์ˆ˜ ์ถ”๊ฐ€
        return self.username  # User object ๋Œ€์‹  ๋‚˜ํƒ€๋‚ผ ๋ฌธ์ž 




    class Meta:
        db_table = 'test_user'


 

Userํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ถœ๋ ฅ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉ์ž์˜ ์ž„์˜๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค. __Str__ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฆฌํ„ด๊ฐ’์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

return ๋’ค์— ์ถœ๋ ฅ๋˜๊ธฐ ์›ํ•˜๋Š”๊ฒƒ์„ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 


 

 

6. ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€ ์„ค์ • (view)

 

ํšŒ์›๋“ฑ๋ก ํ•จ์ˆ˜ ์ƒ์„ฑ

*userํด๋” - views.py

from django.shortcuts import render

# Create your views here.
def register(request):   #ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜
    return render(request, 'register.html') #register๋ฅผ ์š”์ฒญ๋ฐ›์œผ๋ฉด register.html ๋กœ ์‘๋‹ต.

django.shortcuts - render : register ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด, register.html์„ renderํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.    

 

 

 URL ์„ค์ •ํ•˜๊ธฐ

*ํ”„๋กœ์ ํŠธ ํด๋” - urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('user/', include('user.urls'))
    #127.0.0.1:8000/user/ ์ดํ•˜์˜ url๋“ค์€ userํด๋”์˜ urlsํŒŒ์ผ์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค. ๋ผ๋Š” ์„ค์ •
]

ํšŒ์›๊ฐ€์ž…์„ ๋งŒ๋“ค๊ณ  ๋‚˜์„œ์˜ ํŽ˜์ด์ง€๋ฅผ ์–ด๋–ค url์—์„œ ๋‚˜ํƒ€๋‚ด ์ค„์ง€ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

include๊ธฐ๋Šฅ์„ ํ†ตํ•ด userํด๋”์˜ urls ํŒŒ์ผ๋กœ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

 

 

*userํด๋” - urls.py

์•ฑ์„ ์ƒ์„ฑํ›„ ์ดˆ๊ธฐ์—๋Š” urls.py๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ์ƒˆ๋กœ์šด ํŒŒ์ผ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

from django.urls import path
from . import views   
urlpatterns = [
   path('register/',views.register),  
   #์ฆ‰, ์ตœ์ข…์ ์ธ url์€ 127~~~~:8000/user/register๊ฐ€ ๋œ๋‹ค.
] 

 

 

 


 

7. ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€ ํ™”๋ฉด ๋งŒ๋“ค๊ธฐ (HTML ์ž‘์„ฑ)

 

*userํด๋” - templatesํด๋” - register.html

<html>
    
    <head>
        <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

    </head>
    <body>
        <div class= "container">
            <div class = "row mt-5">
                <div class = "col-12 text-center">
                    <h1>ํšŒ์›๊ฐ€์ž…</h1>
                </div>
            </div>
        </div>
        <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    
        
        <!--            ํผ ์•ˆ์—๋‹ค๊ฐ€ ์ž…๋ ฅํ•ด์ค˜์•ผํ•œ๋‹ค. (์•ˆ์จ์ฃผ๋ฉด ์—๋Ÿฌ๋ฐœ์ƒ)
             csrf_token ํผ์˜ ๊ฒฝ์šฐ์—๋Š” , ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์— ์ „๋‹ฌํ•˜๋Š” ๊ธฐ๋Šฅ.์ด๋•Œ ์•”ํ˜ธํ™”๋œ ํ‚ค๋ฅผ ์ˆจ๊ฒจ๋†“์•„ ํฌ๋กœ์Šค๋„๋ฉ”์ธ์„ ๋ง‰๊ธฐ ์œ„ํ•จ. -->
    <!-- bootstrap -component - forms -->
              <div class= "container">
                <div class = "row mt-5">
                    {{error}}
                    <div class = "col-12 ">
            <div class = "row mt-5">
                <div class = "col-12 ">
                    <form method = "POST" action = ".">  
                    {% csrf_token %} 
    <div class="form-group">
      <label for="username">์‚ฌ์šฉ์ž ์ด๋ฆ„</label>
      <input 
      type="text" 
      class="form-control" 
      id="username" 
      placeholder="์‚ฌ์šฉ์ž ์ด๋ฆ„"
      name="username">   
      <!-- ์ด name ๊ฐ’์œผ๋กœ ์ •๋ณด๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค -->
    </div>

    <div class="form-group">
      <label for="password">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
      <input type="password" 
      class="form-control"
       id="password"
        placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ"
        name= "password">
    </div>
    
    <div class="form-group">
      <label for="re_password">๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ</label>
      <input 
      type= "password" 
      class="form-control"
       id="re_password"
        placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ"
        name = "re_password">
    </div>
    <button type="submit" class="btn btn-primary">๋“ฑ๋ก</button>
    </form>    


</body>
</html>

 

 

์‚ฌ์šฉ์ž ์ด๋ฆ„, ๋น„๋ฐ€๋ฒˆํ˜ธ, ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ์— ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ registerํด๋ž˜์Šค๋กœ ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํƒœ๊ทธ๋‚ด๋ถ€์˜ id, name ๊ฐ’์„ ๋ฐ”๊พธ์–ด์ค๋‹ˆ๋‹ค.

{% csrf_token %} :  ํŽ˜์ด์ง€์˜ ํผ์„ ์•”ํ˜ธํ™”ํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ์ดํŠธ์—์„œ ์ˆ˜์ •, ์‚ญ์ œ๋“ฑ์˜ ๊ณต๊ฒฉ์„ ๋ง‰๊ธฐ ์œ„ํ•œ ์žฅ๊ณ ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ† ํฐ์ž…๋‹ˆ๋‹ค.

 

 

userํด๋” - view.py
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.hashers import make_password
from .models import User
# Create your views here.
def register(request):   #ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜
    if request.method == "GET":
        return render(request, 'register.html')

    elif request.method == "POST":
        username = request.POST.get['username',None]   #๋”•์…”๋„ˆ๋ฆฌํ˜•ํƒœ
        password = request.POST.get['password',None]
        re_password = request.POST.get['re_password',None]
        res_data = {} 
        if not (username and password and re_password) :
            res_data['error'] = "๋ชจ๋“  ๊ฐ’์„ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."
        if password != re_password :
            # return HttpResponse('๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.')
            res_data['error'] = '๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.'
        else :
            user = User(username=username, password=make_password(password))
            user.save()
        return render(request, 'register.html', res_data) #register๋ฅผ ์š”์ฒญ๋ฐ›์œผ๋ฉด register.html ๋กœ ์‘๋‹ต.

request.POST.get['username',None] : POST๋กœ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ์˜ 'username'์ด๋ผ๋Š” key๊ฐ’์„ ์ด์šฉํ•˜์—ฌ value๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, value๊ฐ€ ์—†๋‹ค๋ฉด None์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

make_password() : ๊ด„ํ˜ธ ์•ˆ์— ๋“ค์–ด๊ฐ„ ๋ฌธ์ž์—ด์„ ์•”ํ˜ธํ™”ํ•ฉ๋‹ˆ๋‹ค.

res_data : ์œ„ html ํŒŒ์ผ์—์„œ {{error}}์™€ ๋งตํ•‘๋˜์–ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, if๋ฌธ์—์„œ ๊ฑธ๋ฆฌ๋ฉด ๋’ค์˜ ๋ฌธ์ž์—ด์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

 

 

 

*static ํด๋” ์„ค์ •ํ•˜๊ธฐ

ํ”„๋กœ์ ํŠธ ํด๋”์˜ setting.py์—์„œ staticํด๋”๋งŒ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),  # base_dir์€ ํ”„๋กœ์ ํŠธ ํด๋”
]

 

๊ทธ๋ฆฌ๊ณ  ํ”„๋กœ์ ํŠธํด๋” ๋ฐ”๋กœ ํ•˜์œ„์— static ์ด๋ž€ ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ , js cssํŒŒ์ผ๋“ฑ์„ ๋„ฃ๊ณ , ์‚ฌ์šฉํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค.

 

 

 

 

 

 

 

 


 

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.