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?