Templates
Template ์ธ์ด๋ ๊ฐ framework ๋ง๋ค ๋ค๋ฅด๋ค
Django์ template ์ธ์ด๋
DTL
!
DTL (Django Template Language)
Ground Rule
: ์ฐ์ฐ์ DTL์ด ์๋ views.py
์ context
๋ก ๊ณ์ฐ๋ ๊ฒฐ๊ณผ๋ฅผ DTL์ ๋จ์ํ ์ถ๋ ฅํ๋ ์ญํ ๋ง ํ๊ฒ ํ๊ธฐ
๊ธฐ๋ณธ ๋ฌธ๋ฒ
1. ์ถ๋ ฅ {{ }}
{{ }}
{{ menu }}
{{ menu.0 }}
2. ๋ฌธ๋ฒ {% %}
{% %}
{% for menu in menus %}
{% endfor %}
3. ์ฃผ์ ``
{# This is comment #}
๋ฐ๋ณต๋ถ
Loops over each item in an array
{% for reply in replies %}
<p> {{reply}} </p>
{% endfor %}
{{ forloop.counter }}
{{ forloop.counter0 }}
`
`
: ๋ฐฐ์ด์ด ๋น์ด์์ผ๋ฉด ์ถ๋ ฅํ ๋ด์ฉ ์จ์ค ๋ ์ฌ์ฉ
Loop over each item in a dictionary
{% for key, value in data.items %}
{{ key }}: {{ value }}
{% endfor %}
Variable
Description
forloop.counter
The current iteration of the loop (1-indexed)
forloop.counter0
The current iteration of the loop (0-indexed)
forloop.revcounter
The number of iterations from the end of the loop (1-indexed)
forloop.revcounter0
The number of iterations from the end of the loop (0-indexed)
forloop.first
True if this is the first time through the loop
forloop.last
True if this is the last time through the loop
forloop.parentloop
For nested loops, this is the loop surrounding the current one
์กฐ๊ฑด๋ฌธ
{% if user == 'admin' %}
<p> Accessible</p>
{% else %}
<p> Inaccessible</p>
{% endif %}
built-in tag, filter (|
)
|
)๊ณต์๋ฌธ์ ์ฐธ๊ณ ํ์
: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/
length
{{content | length}}
๊ธธ์ด ํ์ธํ๊ธฐ
truncatechars:num
{{content|truncatechars:10}}
10์๋ง ์งค๋ผ์ ๋ณด์ด๊ธฐ
dictsort
{{ value|dictsort:"name" }}
dictionary ์๋ฃํ์ผ๋, ๋ช ์ํ key๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
Template ํ์ฅ
pages/templates/base.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Django Basics - Pages</title>
{% block css %}
{% endblock %}
</head>
<body>
<h1> Django Basic Syntax</h1>
{% block body %}
{% endblock %}
</body>
</html>
posts.html
{% extends 'base.html' %}
{% block css %}
<style>
h1 {
color: blue;
}
</style>
{% endblock %}
{% block body %}
<!-- {# Template language ์์์ ์ฃผ์ #} -->
<h1> Post No. {{id}}</h1>
<p> {{content}}</p>
<p> {{content | length}}</p>
<p> {{content|truncatechars:10}}</p>
<hr>
{% for reply in replies %}
<p> {{forloop.counter}} {{reply}}</p>
{% endfor %}
<hr>
{% for reply in no_replies %}
<p> {{forloop.counter}} {{reply}}</p>
{% empty %}
<p> There's no reply ใ
_ใ
</p>
{% endfor %}
<hr>
{% if user == 'admin' %}
<p> Accessible</p>
{% else %}
<p> Inaccessible</p>
{% endif %}
{% endblock %}
Template ์ค์ - DIR
DIR
TEMPLATES = [
{
# DTL ์์ง์ ํ์ฉ. jinja2 ๋ฑ์ผ๋ก ๋ณ๊ฒฝ ๊ฐ๋ฅํจ
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# APP ๋ด์ ์๋ ํด๋๊ฐ ์๋ ์ถ๊ฐ์ ์ผ๋ก ํ
ํ๋ฆฟ์ผ๋ก ํ์ฉํ๊ณ ์ถ์ ๊ฒฝ๋ก.
'DIRS': [os.path.join(BASE_DIR, 'intro', 'templates')],
# APP_DIRS: True ์ธ๊ฒฝ์ฐ, ๋ฑ๋ก๋ app(INSTALLED_APPS)์ ๋๋ ํ ๋ฆฌ์ ์๋ 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',
],
},
},
]
BASE_DIR
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Linux
,Windows
๋ฑ OS์ ์๊ด์์ด ์ค์ ํ๋ ค๊ณos.path.dirname()
์ผ๋ก ํจ
DIRS ๋ฆฌ์คํธ์ ๊ฒฝ๋ก ์ ์ ํด๋ ๊ตฌ์กฐ๋ฅผ ํตํด ํ์ธํ๊ธฐ
00_django_intro/ <- BASE_DIR
django_intro/
templates/
Multiple Apps
<br
์์ผ๋ก๋ ํญ์ app์ ์์ฑํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ํด๋ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ค.
app1/
templates/
app1/
index.html
a.thml
urls.py
views.py
...
app2/
templates/
app2/
index.html
b.thml
urls.py
views.py
...
1. url ์ค์ ๋ถ๋ฆฌ
๊ฐ๊ฐ์ app ๋ณ๋ก url์ ๊ด๋ฆฌํ๋ค.
ํ๋ก์ ํธ ํด๋ urls.py ์ ์
# django_intro/urls.py urlpatterns = [ path('admin/', admin.site.urls), path('pages/', include('pages.urls')), path('boards/', include('boards.urls')), ]
๊ฐ ํ๋ก์ ํธ๋ณ urls.py ์ ์
from django.urls import path from . import views urlpatterns = [ # /boards/ path('', views.index), # /boards/new/ path('new/', views.new), # /boards/complete/ path('complete/', views.complete), ]
2. templates ํด๋ ๊ตฌ์กฐ
template ํ์ผ์ ๋ฐํํ๊ธฐ ์ํด์ django๋ ์๋์ ํด๋๋ค์ ํ์ํ๋ค.
DIRS ์ ์ ์๋ ๊ฒฝ๋ก์ ํ์ ๋๋ ํ ๋ฆฌ
NSTALLED_APPS ๋๋ ํ ๋ฆฌ์ templates ํด๋์ ํ์ ๋๋ ํ ๋ฆฌ ํ์
์ด ๊ณผ์ ์์ ์ค๋ณต๋ ํ์ผ์ด ์๋ ๊ฒฝ์ฐ, ์์์น ๋ชปํ ๊ฒฐ๊ณผ๊ฐ ๋ํ๋ ์ ์๋ค.
๋ฐ๋ผ์, ์์ผ๋ก ๋ค์๊ณผ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ์ ์งํ๋ค.
app1/
templates/
app1/
app2/
templates/
app2/
Form ์ ํตํ Request ์ฒ๋ฆฌ
์ฌ์ฉ์๋ค๋ก๋ถํฐ ๊ฐ์ ๋ฐ์์ (boards/new/)
๋จ์ ์ถ๋ ฅํ๋ page ๊ตฌ์ฑ (boards/complete/)
1. ์ฌ์ฉ์์๊ฒ form ์์ ์ ๊ณต
1-1 url ์ง์
# boards/urls.py
path('new/', views.new),
1-2 view ํจ์ ์์ฑ
#boards/views.py
def new (request):
return render(request, 'boards/new.html')
1-3 template
<form action="/boards/complete/">
Title: <input type="text" name="title">
</form>
form tag์๋
action
์์ฑ์ ์ ์ํ๋ค์ฌ์ฉ์๋ก๋ถํฐ ๋ด์์ ๋ฐ์์ ์ฒ๋ฆฌํ๋ url
input tag์๋
name
์์ฑ์ ํตํด ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๋ด์ฉ์ ๋ด์ ๋ณ์ ์ด๋ฆ์ ์ง์ ํ๋คurl ์์
/boards/complete/?title="์ ๋ชฉ์ ๋ชฉ"
2. ์ฌ์ฉ์ ์์ฒญ ์ฒ๋ฆฌ
2-1. urls.py ์ ์
bords/url.py
path('/boards/complete', views.complete)
2-2. views.py
boards/views.py
def complete(request):
title = request.GET.get('title')
context = {
'title': title
}
return render(request, 'boards/complete.html', context)
request
์๋ ์์ฒญ๊ณผ ๊ด๋ จ๋ ์ ๋ณด๋ค์ด ๋ด๊ธด object๊ฐ ์ ์ฅ๋์ด ์๋ค
2-3. template
<!-- boards/templates/boards/complete.html -->
{{ title }}
+
Tip) project ์ฝ๊ฒ ๋ง๋ค๊ธฐ!
$ cd intro/
$ django-admin startproject intro .
Last updated
Was this helpful?