Django Template
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
project/settings.py
μ TEMPLATES κ΄λ ¨ μ€μ μ νκ³ μλ κ²μ λ³Ό μ μλ€. μ¬κΈ°μλ μ΄λ»κ² tmeplateμ λΆλ¬μ€κ³ λ λλ§ ν κ²μΈμ§λ₯Ό μ€μ ν μ μλ€.
'APP_DIRS': True
λ INSTALLED_APPS λλ ν 리μ templates νμ λλ ν 리λ₯Ό νμνλ€.
Template νμΌ μμ±νκΈ°
ν
νλ¦Ώμ μμ±ν app λλ ν 리 νμμ templates λλ ν 리λ₯Ό μμ±νλ€. templates λλ ν 리 νμμ polls λλ ν 리 λ΄λΆμ index.html
νμΌμ μμ±ν΄μ€λ€.
polls
βββ __init__.py
βββ admin.py
βββ apps.py
βββ migrations
β βββ 0001_initial.py
β βββ __init__.py
βββ models.py
βββ templates
β βββ polls
β βββ index.html
βββ tests.py
βββ urls.py
βββ views.py
polls/templates/polls/index.html
κ³Ό κ°μ ννκ° λλ€. μ¬κΈ°μ μ templates λλ ν 리 νμμ polls λλ ν 리λ₯Ό μμ±ν΄μ€κ±ΈκΉ?Djangoμμλ μ΄λ¦μ΄ μΌμΉνλ 첫λ²μ§Έ templateμ μ ννλ€. λ§μ½ λμΌν templateμ΄ λ€λ₯Έ μ΄ν리μΌμ΄μ μ μμ κ²½μ°μ μ΄ λμ μ°¨μ΄λ₯Ό ꡬλΆνμ§ λͺ»νλ€. κ°μ₯ μ νν templateμ μ§μ νκΈ° μν΄μ κ°μ₯ νΈλ¦¬ν λ°©λ²μ μ΄λ¦κ³΅κ°μΌλ‘ ꡬλΆμ§λ κ²μ΄λ€.
<!-- polls/index.html -->
{% if last_question_list %}
<ul>
{% for question in last_question_list %}
<li><a href="/polls/{{question.id}}/">{{ question.question_text}}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are avaliable.</p>
{% endif %}
Template μΈμ΄
HTML(μ μ )μ νμ΄μ¬ μ½λ(λμ )λ₯Ό λ°λ‘ λ£μ μ μλ€. template tag{}
λ νμ΄μ¬μ HTMLμΌλ‘ λ°κΎΈμ΄ λΉ λ₯΄κ³ μ½κ² λμ μΈ μΉμ¬μ΄νΈλ₯Ό λ§λ€ μ μλ€.
rubyμμλ
html.erb
{{λ³μ}}
{{λ³μ}}
contextλ‘ μ λ¬λ λ³μλ₯Ό ν νλ¦Ώμμ κ°μ μΆλ ₯νλ €λ©΄ λ€μκ³Ό κ°μ΄ μ€κ΄νΈ μμ λ³μμ΄λ¦μ λ£μ΄μ νμν μ μλ€.
<h1>{{ title }}</h1>
<p>{{ context }}</p>
{%%}
ν
νλ¦Ώ νκ·Έ
{%%}
ν
νλ¦Ώ νκ·Έ`
` νκ·Έ μμλ pythonμ forλ¬Έ, ifλ¬Έκ³Ό κ°μ΄ ν¬νλ¦Ώμ λ‘μ§μ μ μ΄νλ€.
{% if count > 0 %}
Data Count = {{ count }}
{% else %}
No Data
{% endif %}
{% for item in dataList %}
<li>{{ item.name }}</li>
{% endfor %}
{% csrf_token %}
{{ | }}
νν°
{{ | }}
νν°ν
νλ¦Ώ νν°λ λ³μμ κ°μ νΉμ ν ν¬λ§·μΌλ‘ λ³ννλ κΈ°λ₯μ νλ€. μ΄λ |
νμ΄νλ₯Ό μ¬μ©ν΄ νν°λ₯Ό μ μ©νλ€.
μλ₯Ό λ€μ΄, λ μ§λ₯Ό νΉμ λ μ§ ν¬λ§·μΌλ‘ λ³κ²½νκ±°λ, λ¬Έμμ΄μ λ,μλ¬Έμλ‘ λ³κ²½νλ κΈ°λ₯μ ν μ μλ€.
<!--λ μ§ ν¬λ§· μ§μ -->
{{ createDate|date:"Y-m-d" }}
<!--μλ¬Έμ λ³κ²½-->
{{ lastName|lower }}
<!-- λΌμΈ λ³κ²½ -->
{{ post.text|linebreaksbr }}
λ΄μ₯ νν° λ νΌλ°μ€ μμ μ 체 λͺ©λ‘μ λ³Ό μ μμΌλ©°, ν νλ¦Ώ νν°λ₯Ό μ§μ λ§λ€ μ μλ€.
`` μ£Όμ
ν
νλ¦Ώμμ μ£Όμꡬ문μ {# #}
μ΄λ€.
{# μ£Όμ, μ½λμ λν μ€λͺ
#}
HTML Escape
HTML λ΄μ©μ€μ <,>,',",&
κ³Ό κ°μ λ¬Έμλ€μ΄ μμΌλ©΄ κ·Έ λ¬Έμμ μμνλ HTML Entityλ‘ λ³νν΄μ£Όμ΄μΌνλ€.
ν νλ¦Ώμμ μ΄λ¬ν μμ μ μλμΌλ‘ μ²λ¦¬ν΄μ£ΌκΈ° μν΄μ `
ν
νλ¦Ώ νκ·Έλ
escape` ν
νλ¦Ώ νν°λ₯Ό μ¬μ©νλ€.
# autoescape νκ·Έ
{% autoescape on %}
{{ content }}
{% endautoescape %}
# escape νν°
{{ content|escape }}
μ°Έμ‘°
곡μ λ νΌλ°μ€
Last updated
Was this helpful?