๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/Django

Django_์žฅ๊ณ  ๋ถ€์ˆ˜๊ธฐ (2)_MVT ์ƒ์„ฑ

by KASSID 2022. 6. 30.

๋ชฉ์ฐจ

    728x90

    Django Model(๋ชจ๋ธ)

    ๋ชจ๋ธ์€ ๊ฐ์ฒด์˜ ํŠน๋ณ„ํ•œ ์ข…๋ฅ˜์ด๊ณ , ์ด๋ฅผ ์ €์žฅํ•˜๋ฉด ๊ทธ ๋‚ด์šฉ์ด DB์— ์ €์žฅ๋œ๋‹ค.

     

    ์žฅ๊ณ ๋Š” App์˜ ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“ค์–ด์กŒ๋‹ค๊ณ  ํ•  ์ˆ˜์žˆ๋‹ค. ์ฆ‰, ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผํ•œ๋‹ค.

     

    ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์„ฑ

    python manage.py startapp blog(app๋ช…)

     

     django(ํ”„๋กœ์ ํŠธ ์œ„์น˜)
        โ”œโ”€โ”€ mysite
        |       __init__.py
        |       settings.py
        |       urls.py
        |       wsgi.py
        โ”œโ”€โ”€ manage.py
        โ””โ”€โ”€ blog(App ๋””๋ ‰ํ† ๋ฆฌ)
            โ”œโ”€โ”€ migrations
            |       __init__.py
            โ”œโ”€โ”€ __init__.py
            โ”œโ”€โ”€ admin.py
            โ”œโ”€โ”€ models.py
            โ”œโ”€โ”€ tests.py
            โ””โ”€โ”€ views.py

    ์œ„์™€ ๊ฐ™์€ ํŠธ๋ฆฌ๊ฐ€ ์™„์„ฑ์ด ๋  ๊ฒƒ์ด๋‹ค.

     

    ์ƒ์„ฑํ•œ App์— ๋Œ€ํ•˜์—ฌ ๊ผญ ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด ์žˆ๋Š”๋ฐ

    setting.py์—์„œ ํ•ด๋‹น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋“ฑ๋กํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'blog', (์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ถ”๊ฐ€)
    ]

    ์ด ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š์œผ๋ฉด ์žฅ๊ณ ์—์„œ ์–ดํ”Œ์„ ์ธ์‹ํ•˜์ง€ ๋ชปํ•œ๋‹ค.

     

    ๋ชจ๋ธ ์ƒ์„ฑ

    ๋ชจ๋“  Model ๊ฐ์ฒด๋Š” blog/models.py ํŒŒ์ผ์— ์„ ์–ธํ•˜์—ฌ ๋งŒ๋“ ๋‹ค.

    ์ฆ‰, ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ชจ๋ธํŒŒ์ผ์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

     

    ๋ชจ๋ธ์€ class๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •์˜ํ•œ๋‹ค.

    ์ด ๋ชจ๋ธ์˜ ์†์„ฑ ์ฆ‰, ํ•„๋“œ๋งˆ๋‹ค ์–ด๋–ค ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฐ€์ง€๋Š”์ง€ ์ •์˜ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

    ex)

    from django.utils import timezone
    from django.conf import settings
    from django.db import models
    
    class Post(models.Model):
        author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
        title = models.CharField(max_length=200)
        text = models.TextField()
        created_date = models.DateTimeField(default=timezone.now)
        published_date = models.DateTimeField(blank=True, null=True)
    
        def publish(self): # publish ๋ฉ”์„œ๋“œ
            self.published_date = timezone.now()
            self.save()
    
        def __str__(self): # ํฌ์ŠคํŠธ์˜ ์ œ๋ชฉ ๋ฆฌํ„ด
            return self.title

    ๋ชจ๋ธ์˜ ํ•„๋“œ์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    (https://docs.djangoproject.com/en/2.0/ref/models/fields/#field-types)

     

    DB์— ๋ชจ๋ธ์„ ์œ„ํ•œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ

    ๋ชจ๋ธ์„ ์ƒ์„ฑ/์ˆ˜์ •ํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ฐ˜๋“œ์‹œ DB์— ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

    DB์—์„œ๋Š” ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—

    ๊ผญ ์ด ๊ณผ์ •์„ ๊ฑฐ์ณ์„œ ์ธ์‹์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

    python manage.py makemigrations blog(App๋ช…)
    python manage.py migrate blog(App๋ช…)

    makgemigration : ๋ชจ๋ธ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ํ…Œ์ด๋ธ” ์Šคํ‚ค๋งˆ ์ƒ์„ฑ, ์ˆ˜์ •

    migrate : ์‹ค์ œ DB์— ํ…Œ์ด๋ธ” ์ƒ์„ฑ, ์ˆ˜์ •


    Django url

    ๋ชจ๋“  ์ธํ„ฐ๋„ท ํŽ˜์ด์ง€๋Š” ๊ณ ์œ ์˜ url์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

    ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ์‚ฌ์šฉ์ž์˜ request์— ๋“ค์–ด์žˆ๋Š” url์„ ๋ถ„์„ํ•˜๊ณ  ๊ทธ ํŒจํ„ด๊ณผ ๋งค์นญ์„ ์‹œํ‚จ๋‹ค.

     

    ์žฅ๊ณ ๋Š” ์ด๋•Œ URLconf (URL configuration)๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ

    ์ด๋Š” ์žฅ๊ณ ์—์„œ URL๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ทฐ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ํŒจํ„ด๋“ค์˜ ์ง‘ํ•ฉ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    mysite์˜ urls.py๋ฅผ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

    from django.contrib import admin
    from django.urls import path
    
    urlpatterns = [
        path('admin/', admin.site.urls),
    ]

    urlpatterns์— ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ •์˜ํ•œ url์„ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

    from django.urls import path, include
    
    urlpatterns = [
        path('', include('blog.urls')), (app๋ช….urls)
    ]

     

    ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ์˜ url

    ์ด์ œ app ๋””๋ ‰ํ† ๋ฆฌ์—์„œ urls.py๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

    ex)

    from django.urls import path
    from . import views
    
    urlpatterns = [
        path('', views.main, name='main'),
    ]

    ์ด์ œ urlpatterns์— ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•˜๋ฉฐ ํ…œํ”Œ๋ฆฟ๊ณผ ์—ฐ๊ฒฐ์„ ํ•ด์ฃผ๋Š”๋ฐ

    path๋ฅผ ์ด์šฉํ•˜๊ฑฐ๋‚˜ ์ •๊ตํ•œ ์ •์˜๋ฅผ ์œ„ํ•ด์„œ๋Š” re_path() ํ•จ์ˆ˜์™€ ์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.

     


    Django View

    App์˜ ๋กœ์ง์„ ๋„ฃ๋Š” ๊ณณ์œผ๋กœ ๋ชจ๋ธ์—์„œ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€์„œ ํ…œํ”Œ๋ฆฟ(htmlํŒŒ์ผ)์— ์ „๋‹ฌํ•˜๋Š” ์—ญํ• ์ด๋‹ค.

     

    views.py์— ํ•จ์ˆ˜์˜ ํ˜•ํƒœ๋กœ ์ •์˜๋ฅผ ํ•ด์ค€๋‹ค.

    urls.py์— ์žˆ๋Š” views.OO ์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์ด ๋ฐ”๋กœ ์ด๊ฒƒ์ด๋‹ค.

    ์ฆ‰, ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

    ex)

    def main(request):
        posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
        return render(request, 'blog/main.html', {'posts':posts})

    Django Template

    ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ UI ์ฆ‰, htmlํŒŒ์ผ์„ ์ด์•ผ๊ธฐํ•œ๋‹ค.

    ์žฅ๊ณ ์—์„œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์„ ์ฐพ์„ ๋•Œ๋Š” setting.py์— ๋“ฑ๋ก๋˜์–ด์žˆ๋Š”

    TEMPLATE_DIRS ๋ฐ INSTALLED_APPS์—์„œ ์ง€์ •๋œ ์•ฑ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค.

     

    ๋ณดํ†ต ์•ฑ ๋‚ด์˜ template ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๊ทธ ๊ณณ์— ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.

    ๋Œ“๊ธ€