1. Django Tutorial(Airbnb) - Url & View & Templates

ID์งฑ์žฌยท2021๋…„ 8์›” 4์ผ
0

Django

๋ชฉ๋ก ๋ณด๊ธฐ
15/43
post-thumbnail

๐ŸŒˆ Url & View & Templates

๐Ÿ”ฅ About Url and View

๐Ÿ”ฅ About View And Templates


1. About Url and View

  • ์‚ฌ์šฉ์ž๊ฐ€ Django์—๊ฒŒ ์š”์ฒญํ•œ url์„ ๋ถ„์„ํ•˜์—ฌ ๋ฐ˜์‘ํ•  ๋กœ์ง์„ ๋งคํ•‘ํ•˜๋Š” ๊ณณ์ด urls.py์ด๊ณ , Django์—์„œ ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋Š” Function ๋˜๋Š” Class๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ณณ์ด views.py์—์š”!
  • ์ด์— urls.py๋Š” ์š”์ฒญ๋œ request์— ๋”ฐ๋ผ view.py์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , views.py๋Š” DB์™€ ์†Œํ†ตํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฌผ์„ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋‹ค์‹œ ์ „๋‹ฌํ•ด์ค๋‹ˆ๋‹ค.
  • ์—ฌ๊ธฐ์„œ ๊ฒฐ๊ณผ๋ฌผ๋“ค์ด๋ž€ ์–ด๋–ค ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋˜๊ฐ€, ์–ด๋–ค ํŽ˜์ด์ง€๋ฅผ ๋„์šฐ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์— ๋”ฐ๋ฅธ ์‘๋‹ต์„ ์˜๋ฏธํ•ด์š”!

1) urlpatterns

  • Django ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด urls.py๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ๊ธด ๋‹ต๋‹ˆ๋‹ค. ์ด๋ฏธ ๋ฌด์–ธ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ์š”. "http://127.0.0.1:8000/admin/" ๋กœ ์ ‘๊ทผํ•˜๋ฉด admin site๊ฐ€ ์‘๋‹ตํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฐ๊ฒฐ์‹œ์ผœ๋‘” ๊ฒƒ์ด์ฃ .
  • ์ด์ฒ˜๋Ÿผ path("๊ฒฝ๋กœ", "์‹คํ–‰ ๋กœ์ง") ํ˜•ํƒœ๋กœ ์–ด๋–ค url์ด ์š”์ฒญ๋˜๋ฉด, ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ฌ์ง€ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.
urlpatterns = [
    path("admin/", admin.site.urls),
]

2) include

  • urls.py์— ๋ชจ๋“  ๊ฒฝ๋กœ์— ๋Œ€ํ•ด ํ•จ์ˆ˜๋ฅผ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ๊ทธ๋Ÿผ ๋งค์šฐ ๋ณต์žกํ•ด์ ธ ๊ธฐ๋Šฅ์ด ๋งŽ์•„์งˆ ์ˆ˜๋ก ๋ณต์žก๋„๊ฐ€ ์˜ฌ๋ผ๊ฐ‘๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด App๋ณ„๋กœ urls.py๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ถ„๋ฆฌํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹ค๋งŒ, ์š”์ฒญ๋œ url์„ ์–ด๋Š App์— urls.py๋กœ ๋ณด๋‚ผ์ง€๋Š” ์•Œ ์ˆ˜ ์žˆ์–ด์•ผ ๊ฒ ์ฃ ? ์ด๋Ÿด ๋•Œ, ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด include ๋ž๋‹ˆ๋‹ค.
  • "path("", include("core.urls"))," ๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ์š”, ""๊ฐ€ ๋œปํ•˜๋Š” ์˜๋ฏธ๋Š” ๋ฉ”์ธ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. url ๋’ค์— ์–ด๋–ค ๊ฒƒ๋„ ๋ถ™์–ด์žˆ์ง€ ์•Š์€ ๊ฒฝ๋กœ์ด์ฃ . ๋งˆ์น˜ https://naver.com, https://google.com์™€ ๊ฐ™์€๊ฑฐ์—์š”!
  • ์ฆ‰, ๋ฉ”์ธ ํŽ˜์ด์ง€๊ฐ€ ์š”์ฒญ๋˜์—ˆ์„ ๋•Œ, core/urls.py์—์„œ ์ž‘๋™๋  ํ•จ์ˆ˜๋ฅผ ์ฐพ์•„๋ณด๋ž€ ์˜๋ฏธ์ด์ฃ .
    • ๐Ÿ”Ž path("", include("์ฐธ์กฐํ•  urls.py ๊ฒฝ๋กœ"))
from django.contrib import admin
from django.urls import path, include # ๐Ÿ‘ˆ include๋Š” path์™€ ๊ฐ™์€ ๊ณณ์— ์œ„์น˜ํ•ด ์žˆ์–ด์š”.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path("", include("core.urls")), # ๐Ÿ‘ˆ url("")์ด ์š”์ฒญ๋˜๋ฉด, core/urls.py๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”:)
    path("admin/", admin.site.urls),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • ์ด์ œ core/urls.py์—์„œ ""๊ฒฝ๋กœ์— ๋ฐ˜์‘ํ•  ํ•จ์ˆ˜๋ฅผ ๋งคํ•‘ํ•˜๋ฉด ๋˜๋Š”๋ฐ์š”, ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ ์ ‘๊ทผํ•˜๋ฉด rooms/views.py์˜ all_rooms ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋„๋ก ํ•˜์˜€์–ด์š”.
from django.urls import path
from rooms import views as room_views
urlpatterns = [
    path("", room_views.all_rooms)
]

3) request

  • ์‚ฌ์šฉ์ž๊ฐ€ ์„œ๋ฒ„์— url์„ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ์‚ฌ์šฉ์ž๊ฐ€ form์— ๋ฌด์—‡์„ ์ž…๋ ฅํ–ˆ๋Š”์ง€, ๋กœ๊ทธ์ธ๋œ ์ƒํƒœ์ธ์ง€ ๋“ฑ์˜ ์š”์ฒญ๊ณผ ๊ด€๋ จ๋œ ์ •๋ณด๋“ค์„ Django๊ฐ€ Python object๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ request์— ๋‹ด์•„ ์ค๋‹ˆ๋‹ค:) ์ด์—,, ์š”์ฒญ๊ณผ ๊ด€๋ จํ•œ ์ •๋ณด๋ฅผ request์— ๋‹ด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— request๋ฅผ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›์•„์ค๋‹ˆ๋‹ค.
from django.shortcuts import render
def all_rooms(request):
    # print(dir(request)) ๐Ÿ‘ˆ ์–ด๋–ค ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ์–ด์š”
    pass

4) HttpResponse()

  • rooms/views.py์—์„œ all_rooms ํ•จ์ˆ˜๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ, ๋ฉ”์ธํŽ˜์ด์ง€("")์˜ ๊ฒฝ๋กœ์ธ http://127.0.0.1:8000/๋กœ ์ ‘๊ทผํ•ด ๋ณผ๊นŒ์š”?

  • HttpResponse๋กœ returnํ•˜๋ผ๊ณ  ์นœ์ ˆํ•˜๊ฒŒ ์•Œ๋ ค์ฃผ๋Š”๋ฐ์š”, ๊ทธ๋Ÿผ HttpResponse์— "Hello Django!"๋ฅผ ๋ฐ˜ํ™˜ํ•ด ๋ณด์ฃ .
    • ๐Ÿ”Ž from django.http import HttpResponse
    • ๐Ÿ”Ž return HttpResponse("๋‚ด์šฉ")
from django.shortcuts import render
from django.http import HttpResponse # ๐Ÿ‘ˆ HttpResponse๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ์ด์—์š”
def all_rooms(request):
    return HttpResponse("Hello Django!")
  • ๋ฉ”์ธ ํŽ˜์ด์ง€(http://127.0.0.1:8000/)์— "Hello Django"๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ์–ด์š”!
  • HttpResponse๋Š” ์ ‘๊ทผ๋œ url์˜ ์š”์ฒญ์— ๋”ฐ๋ฅธ ๊ฐ„๋‹จํ•œ ์‘๋‹ต(response)์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.
from datetime import datetime
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def all_rooms(request):
    now = datetime.now()
    return HttpResponse(content=f"<h1>{now}</h1>")

5) namespace & name

  • namespace๋Š” includeํ•œ urls.py๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ธฐ ์œ„ํ•œ ๋ณ„์นญ์œผ๋กœ ์‚ฌ์šฉ๋˜์š”. html์—์„œ aํƒœ๊ทธ๋ฅผ ํ†ตํ•ด ๋งํฌ๋ฅผ ์—ฐ๊ฒฐ์‹œํ‚ฌ ๋•Œ, ๊ฒฝ๋กœ๋ฅผ ์ง์ ‘ ์—ฐ๊ฒฐํ•˜๋ฉด ๋งคํ•‘ํ•  url ์ฃผ์†Œ๊ฐ€ ์ˆ˜์ •๋  ๋•Œ ๋งˆ๋‹ค ํ…œํ”Œ๋ฆฟ์—๊ฐ€์„œ ๋ชจ๋“  ๊ณณ์„ ์ฐพ์•„ ์ˆ˜์ •ํ•ด์•ผํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์„ ์ค„์—ฌ์ค๋‹ˆ๋‹ค.
  • ์ด์— namespace ์‚ฌ์šฉํ•จ์œผ๋กœ์จ, ์ฐธ์กฐํ•  urls.py๋ฅผ ๋ฐ”๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์–ด์š”.
  • namespace๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด, ํ•ด๋‹น urls.py์— ๊ฐ€์„œ app_name์„ namespace ๊ฐ’๊ณผ ์ผ์น˜์‹œ์ผœ์ค˜์•ผ ํ•ด์š”.
# url.spy
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path("", include("core.urls", namespace="core")), # ๐Ÿ‘ˆ core/urls.py์— app_name์„ core๋กœ ์ผ์น˜์‹œ์ผœ์ค˜์š”:)
    path("rooms/", include("rooms.urls", namespace="rooms")), # ๐Ÿ‘ˆ rooms/urls.py์— app_name์„ rooms๋กœ ์ผ์น˜์‹œ์ผœ์ค˜์š”:)
    path("admin/", admin.site.urls),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • name์€ urlpatterns ์ค‘ ์–ด๋–ค path๋ฅผ ๊ฐ€๋ฅดํ‚ฌ์ง€ ๋ณ„์นญ์„ ์ง€์ •ํ•˜๋Š” ์—ญํ• ์ด์—์š”.
  • ํ…œํ”Œ๋ฆฟ์—์„œ namespace ๋ฐ name์„ ์ง€์ •ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ์•„๋ž˜์ฒ˜๋Ÿผ ์ง์ ‘ ๊ฒฝ๋กœ๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • <a href="/"></a>
    • <a href="/rooms/{{room.pk}}"></a>
  • namespace์™€ name์„ ์ง€์ •ํ–ˆ๋‹ค๋ฉด, ๋ณ„์นญ์œผ๋กœ ๊ฐ€๋ฅดํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ๋งคํ•‘๋œ url๊ฐ’์ด ๋ณ€ํ•˜๋”๋ผ๋„ ํ•ด๋‹น path๋ฅผ ๊ฐ€๋ฅดํ‚ฌ ์ˆ˜ ์žˆ์–ด์š”!
    • <a href="{% url "core:home" %}"></a>
    • <a href="{% url "rooms:detaisl" room.pk %}"></a>
# core/urls.py
from django.urls import path
from rooms import views as room_views
app_name = "core" # ๐Ÿ‘ˆ namespace์™€ ๊ฐ™์€ ๊ฐ’์„ ์ผ์น˜์‹œ์ผœ์•ผ ํ•ด์š”:)
urlpatterns = [
    path("", room_views.HomeView.as_view(), name="home"), # ๐Ÿ‘ˆ name์€ ํ•ด๋‹น path์˜ ๋ณ„์นญ์ด์—์š”:)
]
# rooms/urls.py
from django.urls import path
from . import views
app_name = "rooms" # ๐Ÿ‘ˆ namespace์™€ ๊ฐ™์€ ๊ฐ’์„ ์ผ์น˜์‹œ์ผœ์•ผ ํ•ด์š”:)
urlpatterns = [
    path("<int:pk>", views.room_detail, name="detail"), # ๐Ÿ‘ˆ name์€ ํ•ด๋‹น path์˜ ๋ณ„์นญ์ด์—์š”:)
]

2. About View And Templates

1) render()

  • ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์ด ๋งŽ์•„์งˆ ์ˆ˜๋ก ์›น๋ธŒ๋ผ์šฐ์ €์— ๋‚˜ํƒ€๋‚ผ ๋ชจ๋“  ๋‚ด์šฉ์„ HttpResponse๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ์‘๋‹ตํ•˜๊ธฐ ๋ณด๋‹ค html์„ ๋ณ„๋„๋กœ ๊ด€๋ฆฌํ•˜๊ณ  content๋ฅผ ํ•œ๋ฒˆ์— ์‘๋‹ตํ•˜๋Š”๊ฒŒ ๊ฐ„ํŽธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด "render()" ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
from django.shortcuts import render
def all_rooms(request):
    return render(request, "all_rooms.html")
  • Django๊ฐ€ "all_rooms.html" ํ…œํ”Œ๋ฆฟ์„ ์ฐพ์•„๋ณด๋ คํ–ˆ์œผ๋‚˜, ์กด์žฌํ•˜์ง€ ์•Š๋‹ค๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์ธ๋ฐ์š”,, ์•„์ง "all_rooms.html" ํ…œํ”Œ๋ฆฟ์ด์„ ๋งŒ๋“ค์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด์—์š”!
  • Django์˜ templates ์—”์ง„์ด ๊ณ„์† templates ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ฃผ์‹œํ•˜๋‹ค ์š”์ฒญ๋œ html์„ ์ „๋‹ฌํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— templates ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์— html ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋‘์–ด์•ผ ํ•ด์š”!
  • App ์•ˆ์— templates ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ ํ›„ "all_rooms.html"์„ ๋งŒ๋“ค์–ด "Hello Django!"๋ฅผ ์ž‘์„ฑํ•˜๋ฉด htmlํŒŒ์ผ์„ renderํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค:)
    • ๐Ÿ”Ž "all_rooms.html" โ†’ <h1>Hello Django!</h1>
  • App ๋ณ„๋กœ templates ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋‘์ง€ ์•Š๊ณ , ํ”„๋กœ์ ํŠธ ๋ฐ”๋กœ ์•„๋ž˜์— templates ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด html ํŒŒ์ผ์„ ๋ชจ๋‘ ์ด ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ๊ด€๋ฆฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, settgins.py์˜ ์„ค์ •์ด ํ•„์š”ํ•ด์š”. BASE_DIR ์•„๋ž˜์˜ templates ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์„ ์ฐพ์•„๋ณด๋ผ๊ณ  ์•„๋ž˜์ฒ˜๋Ÿผ ์•Œ๋ ค์ค„ ์ˆ˜ ์žˆ์ฃ !
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [os.path.join(BASE_DIR, "templates")], ๐Ÿ‘ˆ ์š”๊ธฐ์—์š”
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

2) context

  • render์˜ context ์†์„ฑ์„ ํ†ตํ•ด html์— ๋ณ€์ˆ˜๋„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์–ด์š”. context์— dictํ˜•ํƒœ๋กœ ๋‹ด์•„ ์ „๋‹ฌํ•˜๋ฉด, html์—์„œ ์ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ฃ .
  • DB์˜ ๋ชจ๋“  ๋ฐฉ์„ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ํ•ด๋‹น ๋ชจ๋ธ์„ importํ•˜๊ณ , ๋ชจ๋“  Object๋ฅผ dictํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•˜๊ธฐ๋งŒํ•˜๋ฉด ๋˜์š”.
    • ๐Ÿ”Ž all_rooms = models.Room.objects.all()
from django.shortcuts import render
from . import models
def all_rooms(request):
    all_rooms = models.Room.objects.all()
    context = {"rooms": all_rooms}
    return render(request, "all_rooms.html", context)
  • html์—์„œ ์ด ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋”•์…”๋„ˆ๋ฆฌ์— key๊ฐ’์„ ์ด์šฉํ•˜๋ฉด ๋˜๋Š”๋ฐ์š”, {{}}์•ˆ์— ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด, Django์˜ ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜๋กœ ๊ธฐ๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ๋˜ํ•œ for๋ฌธ, if๋ฌธ์€ {% %}์œผ๋กœ ๊ฐ์‹ธ์„œ python ์ฝ”๋“œ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค!
{% for room in rooms %}
<h1>{{room.name}} / ${{room.price}}</h1>
{%endfor%}

3) block & extends

  • Templates์„ ๋งŒ๋“ค๋‹ค๋ณด๋ฉด, <head></head>, <nav></nav> ๋“ฑ ๋ฐ˜๋ณต์ ์ธ ์˜์—ญ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ณตํ†ต์ ์œผ๋กœ ํ•„์š”๋กœํ•˜๋Š” html ์˜์—ญ์„ ๋ณ„๋„๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ์ด๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ๋ณต์žก์„ฑ์ด ์ค„์–ด๋“ค๊ณ  ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ํŽธ๋ฆฌํ•ด์ง‘๋‹ˆ๋‹ค.
  • Python์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ Django์˜ Templates Inheritance ๊ธฐ๋Šฅ์œผ๋กœ html์„ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค.
  • ์ด์— ๊ฐ Templates์— ๊ณตํ†ต์œผ๋กœ ์กด์žฌํ•  html ์ฝ”๋“œ๋ฅผ "base.html"์— ๋งŒ๋“  ๋’ค, ๊ฐ Templates์—์„œ Django์˜ Templates Inheritance ๊ธฐ๋Šฅ์œผ๋กœ base.html์„ ์ƒ์†ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์š”:)
  • ๋ถ€๋ชจ ํ…œํ”Œ๋ฆฟ(base.html)์—์„œ block ์˜์—ญ์€ ์ž์‹ ํ…œํ”Œ๋ฆฟ์˜ html์ฝ”๋“œ๊ฐ€ ์–ด๋Š ๋ถ€๋ถ„์— ์œ„์น˜ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•ด์š”.
    • ๐Ÿ”Ž "{% block [block ์ด๋ฆ„] %} {% endblock %}"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block page_title %}{% endblock page_title %}| Nbnb</title>
</head>  
<body>
    {% block content %} ๐Ÿ‘ˆ content๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ block์˜์—ญ์„ ์œ„์น˜์‹œ์ผœ์คฌ์–ด์š”:)
    {% endblock content %}
</body>  
</html>
  • ์ž์‹ ํ…œํ”Œ๋ฆฟ์—์„œ๋Š” ์ƒ์† ๋ฐ›์„ ๋ถ€๋ชจ ํ…œํ”Œ๋ฆฟ์„ extends๋ฅผ ํ†ตํ•ด ๋ช…์‹œํ•˜๊ณ , base.html์— block์˜์—ญ์ด ์—ฌ๋Ÿฌ๊ฐœ ์กด์žฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น html์ฝ”๋“œ๊ฐ€ base.html์˜ ์–ด๋–ค ๋ถ€๋ถ„์˜ block์˜์—ญ์„ ๊ฐ€๋ฅดํ‚ค๋Š”์ง€ ์ง€์ •ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • ๐Ÿ”Ž {% extends "๋ถ€๋ชจ ํŒŒ์ผ๋ช….html" %}
    • ๐Ÿ”Ž {% block [block์ด๋ฆ„] %} ~ {% endblock [block์ด๋ฆ„] %}
{% extends "base.html" %}
    {% block page_title %}
        Home
    {% endblock page_title %}
    {% block content %}
        {% for room in rooms %}
            <h1>{{room.name}} / ${{room.price}}</h1>
        {% endfor %}
    {% endblock content %}

4) include

  • ๋ถ€๋ชจ ํ…œํ”Œ๋ฆฟ์ธ "base.html" ๋˜ํ•œ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๊ฒŒ ๋˜๋ฉด, ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ํž˜๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด๋ฅผ include๋ผ ํ•˜๊ณ , "base.html"์˜ <header></header>, <footer></footer> ๋“ฑ์„ ๋ณ„๋„์˜ html๋กœ ๋ถ„๋ฆฌ์‹œํ‚จ๋’ค, "base.html"๋กœ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค:)
  • ์ฆ‰, html ํŒŒ์ผ์˜ ์ฝ”๋“œ๋ฅผ ๋ถ„๋ฆฌ์‹œํ‚จ ํ›„ include๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ๋งˆ์น˜ ํ•˜๋‚˜์˜ Template์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ด์—์š”:)
# base.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block page_name %}{% endblock page_name %}| Nbnb</title>
</head>
<body>
    {% include "partials/header.html" %} ๐Ÿ‘ˆ header.html์˜ html ์ฝ”๋“œ๋ฅผ ์—ฌ๊ธฐ์— ์œ„์น˜์‹œ์ผœ์š”!
    {% block content %}{% endblock content %}
    {% include "partials/footer.html" %} ๐Ÿ‘ˆ footer.html์˜ html ์ฝ”๋“œ๋ฅผ ์—ฌ๊ธฐ์— ์œ„์น˜์‹œ์ผœ์š”!
</body>
</html>
  • include๋Š” ํฌํ•จ์‹œํ‚ฌ ํ…œํ”Œ๋ฆฟ์—๋งŒ ์„ ์–ธํ•˜๋ฉด ๋˜๊ธฐ ๋–„๋ฌธ์— ํฌํ•จ์ด ๋˜์–ด์ง€๋Š” ํ…œํ”Œ๋ฆฟ์€ ๋ณ„๋„๋กœ ์กฐ์น˜ํ• ๊ฒŒ ์—†๋‹ต๋‹ˆ๋‹ค!
# partials/header.html
<header>
    <a href="/">Nbnb</a>
    <ul>
        <li><a href="#">Login</a></li>
    </ul>
</header>  
# partials/footer.html
<footer>
    &copy; Nbnb
</footer>
  • include๋  ํ…œํ”Œ๋ฆฟ์œผ๋กœ ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด with์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.
{% for room in rooms %}
	{% include 'partials/room_card.html' with room=room %} # ๐Ÿ‘ˆ with ์‚ฌ์šฉ
{% endfor %}
profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€