Templates
Template μΈμ΄λ κ° framework λ§λ€ λ€λ₯΄λ€
Djangoμ template μΈμ΄λ
DTL
!
DTL (Django Template Language)
Ground Rule
: μ°μ°μ DTLμ΄ μλ views.py
μ context
λ‘ κ³μ°λ κ²°κ³Όλ₯Ό DTLμ λ¨μν μΆλ ₯νλ μν λ§ νκ² νκΈ°
κΈ°λ³Έ λ¬Έλ²
1. μΆλ ₯ {{ }}
{{ }}
2. λ¬Έλ² `{% %}
`
3. μ£Όμ
λ°λ³΅λ¬Έ
Loops over each item in an array
λ°°μ΄μ΄ λΉμ΄μμΌλ©΄ μΆλ ₯ν λ΄μ© μ¨μ€ λ μ¬μ©
Loop over each item in a dictionary
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
쑰건문
built-in tag, filter (|
)
|
)곡μλ¬Έμ μ°Έκ³ νμ
: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/
length
κΈΈμ΄ νμΈνκΈ°
truncatechars:num
10μλ§ μλΌμ 보μ΄κΈ°
dictsort
dictionary μλ£νμΌλ, λͺ μν keyλ₯Ό κΈ°μ€μΌλ‘ μ λ ¬
Template νμ₯
pages/templates/base.html
posts.html
Template μ€μ - DIR
DIR
BASE_DIR
Linux
,Windows
λ± OSμ μκ΄μμ΄ μ€μ νλ €κ³os.path.dirname()
μΌλ‘ ν¨
DIRS 리μ€νΈμ κ²½λ‘ μ μ ν΄λ ꡬ쑰λ₯Ό ν΅ν΄ νμΈνκΈ°
Multiple Apps
<br
μμΌλ‘λ νμ appμ μμ±νλ©΄ λ€μκ³Ό κ°μ ν΄λ ꡬ쑰λ₯Ό κ°μ§λ€.
1. url μ€μ λΆλ¦¬
κ°κ°μ app λ³λ‘ urlμ κ΄λ¦¬νλ€.
νλ‘μ νΈ ν΄λ urls.py μ μ
κ° νλ‘μ νΈλ³ urls.py μ μ
2. templates ν΄λ ꡬ쑰
template νμΌμ λ°ννκΈ° μν΄μ djangoλ μλμ ν΄λλ€μ νμνλ€.
DIRS μ μ μλ κ²½λ‘μ νμ λλ ν 리
NSTALLED_APPS λλ ν 리μ templates ν΄λμ νμ λλ ν 리 νμ
μ΄ κ³Όμ μμ μ€λ³΅λ νμΌμ΄ μλ κ²½μ°, μμμΉ λͺ»ν κ²°κ³Όκ° λνλ μ μλ€.
λ°λΌμ, μμΌλ‘ λ€μκ³Ό κ°μ ꡬ쑰λ₯Ό μ μ§νλ€.
Form μ ν΅ν Request μ²λ¦¬
μ¬μ©μλ€λ‘λΆν° κ°μ λ°μμ (boards/new/)
λ¨μ μΆλ ₯νλ page κ΅¬μ± (boards/complete/)
1. μ¬μ©μμκ² form μμ μ 곡
1-1 url μ§μ
1-2 view ν¨μ μμ±
1-3 template
form tagμλ
action
μμ±μ μ μνλ€μ¬μ©μλ‘λΆν° λ΄μμ λ°μμ μ²λ¦¬νλ url
input tagμλ
name
μμ±μ ν΅ν΄ μ¬μ©μκ° μ λ ₯ν λ΄μ©μ λ΄μ λ³μ μ΄λ¦μ μ§μ νλ€url μμ
/boards/complete/?title="μ λͺ©μ λͺ©"
2. μ¬μ©μ μμ² μ²λ¦¬
2-1. urls.py μ μ
bords/url.py
2-2. views.py
boards/views.py
request
μλ μμ²κ³Ό κ΄λ ¨λ μ 보λ€μ΄ λ΄κΈ΄ objectκ° μ μ₯λμ΄ μλ€
2-3. template
+
Tip) project μ½κ² λ§λ€κΈ°!
Last updated
Was this helpful?