Django ๊ธฐ์ดˆ - Template Language

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

Django

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

๐ŸŒˆ Template Language

๐Ÿ”ฅ Template ํƒœ๊ทธ : {% ๋ฌธ๋ฒ• %}

๐Ÿ”ฅ Template ๋ณ€์ˆ˜ : {{ ๋ณ€์ˆ˜ }}

๐Ÿ”ฅ Template ํ•„ํ„ฐ : {{ ๋ณ€์ˆ˜|์˜ต์…˜ }}


  • Template Language๋กœ๋Š” Template ํƒœ๊ทธ, Template ๋ณ€์ˆ˜, Template ํ•„ํ„ฐ๊ฐ€์žˆ๋Š”๋ฐ์š”,, ์ด Template Language ํ†ตํ•ด python ์ฝ”๋“œ๋ฅผ html ํƒฌํ”Œ๋ฆฟ ์•ˆ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์–ด์š”!
  • Template ํƒœ๊ทธ๋Š” html๋ฌธ์„œ์—์„œ python code๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒํ•˜๋Š” ๋ฌธ๋ฒ•์ด๊ณ , Template ๋ณ€์ˆ˜๋กœ๋Š” view๋ฅผ ํ†ตํ•ด ์ „๋‹ฌํ•œ ๋ณ€์ˆ˜๋ฅผ ํ…œํ”Œ๋ฆฟ ์•ˆ์—์„œ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์–ด์š”.
  • Template ํ•„ํ„ฐ๋Š” ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜ ์•ˆ์— ๋‹ด๊ธด ๋ฌธ์ž์—ด์ด๋‚˜ ๋ฆฌ์ŠคํŠธ ๋“ฑ์„ ์‰ฝ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๊ณ  ์žˆ์–ด์š”.
  • ์ฐธ๊ณ  : https://docs.djangoproject.com/en/3.2/ref/templates/builtins/

2. Template ํƒœ๊ทธ : {% ๋ฌธ๋ฒ• %}

1) {% for in %} ~ {% endfor %}

  • ํƒฌํ”Œ๋ฆฟ ํƒœ๊ทธ์„ ํ†ตํ•ด python ๋ฌธ๋ฒ•์˜ if, else, for๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ ,, extends, block, include ๋“ฑ์„ ํ†ตํ•ด ๋‹ค๋ฅธ ํ…œํ”Œ๋ฆฟ์„ ์ƒ์†๋ฐ›๊ฑฐ๋‚˜ ํฌํ•จ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค.
  • {{forloop.counter}}๋Š” ๋ฐ˜๋ณต๋ฌธ์•ˆ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ˜๋ณต ํšŸ์ˆ˜๋งŒํผ countํ•ด์ค๋‹ˆ๋‹ค.
    • ๐Ÿ” {{forloop.counter}} : 1๋ถ€ํ„ฐ ๋ฐ˜๋ณตํšŸ์ˆ˜๋งŒํผ count
    • ๐Ÿ” {{forloop.counter0}} : 0๋ถ€ํ„ฐ ๋ฐ˜๋ณตํšŸ์ˆ˜๋งŒํผ count
    • ๐Ÿ” {{forloop.revcounter}} : ์ „์ฒด ๊ธธ์ด๋กœ ์‹œ์ž‘ํ•ด์„œ 1๊นŒ์ง€ count
    • ๐Ÿ” {{forloop.revcounter0}} : ์ „์ฒด ๊ธธ์ด๋กœ ์‹œ์ž‘ํ•ด์„œ 0๊นŒ์ง€ count
from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from .models import Notice # ๐Ÿ‘ˆ models.py์— Model(Notice)๋ฅผ ๊ฐ€์ ธ์˜ด
# Create your views here.
def sample(request):
    data = Notice.objects.all() # ๐Ÿ‘ˆ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ data์— ๋‹ด์Œ
    return render(request, 'sample.html', {'value':data}) ๐Ÿ‘ˆ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์ „๋‹ฌ
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
  </head>
  <body>
    <h1>Template Language!</h1>
    <p>{{value}}</p>
    {% for i in value %}
    <div>๋ฒˆํ˜ธ : {{forloop.counter}} ๋ฒˆ์งธ data</div>
    <div>์ œ๋ชฉ : {{i.title}}</div>
    <div>์ถ”์ฒœ์ˆ˜ : {{i.likeCount}}</div>
    <div>์กฐํšŒ์ˆ˜ : {{i.viewCount}}</div>
    <div>๋‚ด์šฉ : {{i.contents}}</div>
    <br />
    {% endfor %}
  </body>
</html>

2) {% if %} .. {% elif %} .. {% else %} .. {% endif %}

  • ํƒฌํ”Œ๋ฆฟ ํƒœ๊ทธ๋ฅผ ํ†ตํ•ด if, elif, else๋ฌธ๋„ ์•„๋ž˜ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์š”!
{%load static%}
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
  </head>
  <body>
    <h1>Template Language!</h1>
    <p>{{value}}</p>
    {% for i in value %}
    <div>{{forloop.counter}} ๋ฒˆ์งธ data</div>
    <div>์ œ๋ชฉ : {{i.title}}</div>
    <div>์ถ”์ฒœ์ˆ˜ : {{i.likeCount}}</div>
    <div>์กฐํšŒ์ˆ˜ : {{i.viewCount}}</div>
    <div>๋‚ด์šฉ : {{i.contents}}</div>
    {% if i.viewCount > 10 %}
    <p>์กฐํšŒ์ˆ˜๊ฐ€ 10์ด์ƒ ์ž…๋‹ˆ๋‹ค.</p>
    {% elif i.viewCount > 5 %}
    <p>์กฐํšŒ์ˆ˜๊ฐ€ 5์ด์ƒ 10๋ฏธ๋งŒ ์ž…๋‹ˆ๋‹ค.</p>
    {% else %}
    <p>์กฐํšŒ์ˆ˜๊ฐ€ 5๋ฏธ๋งŒ ์ž…๋‹ˆ๋‹ค.</p>
    {% endif %}
    <br />
    {% endfor %}
  </body>
</html>

3) {% with ๋ณ€์ˆ˜๋ช… = '๊ฐ’' %}

  • ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด html์•ˆ์—์„œ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ ๋’ค ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค!
{%load static%}
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
  </head>
  <body>
    <h1>Template Language!</h1>
    {% with value='hello Django'%}  ๐Ÿ‘ˆ "hello Django"๋ฅผ value๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด์•˜์–ด์š”:)
    <h4>{{value}}</h4>
    {% endwith %}
  </body>
</html>


3. Template ๋ณ€์ˆ˜ : {{ ๋ณ€์ˆ˜ }}

  • view์—์„œ ๋ณ€์ˆ˜์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์ „๋‹ฌํ•˜๋ฉด html ํŒŒ์ผ ์•ˆ์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์š”. Dictํ˜•, listํ˜• ๋ชจ๋‘ ์ „๋‹ฌ ๊ฐ€๋Šฅ ํ•ฉ๋‹ˆ๋‹ค.
# views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
def sample(request):
    d = {'name':'jaewon', 'age':99, 'location':'seoul', 'isman':True}
    l = [100,200,300]
    return render(request, 'sample.html', {'d':d, 'l':l})
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
  </head>
  <body>
    <h1>Template Language!</h1>
    <<p>
      <div>l ์ถœ๋ ฅ : {{l}}</div>
      <div>l์˜ 0๋ฒˆ์งธ ๊ฐ’์€ : {{l.0}}</div>
      <div>l์˜ 1๋ฒˆ์งธ ๊ฐ’์€ : {{l.1}}</div>
      <div>l์˜ 2๋ฒˆ์งธ ๊ฐ’์€ : {{l.2}}</div>
    </p>
    <p>
      <div>d ์ถœ๋ ฅ : {{d}}</div>
      <div>d์˜ key(name) ๊ฐ’์€ : {{d.name}}</div>
      <div>d์˜ key(age)) ๊ฐ’์€ : {{d.age}}</div>
      <div>d์˜ key(location) ๊ฐ’์€ : {{d.location}}</div>
      <div>d์˜ key(isman) ๊ฐ’์€ : {{d.isman}}</div>
    </p>
  </body>
</html>


4. Template ํ•„ํ„ฐ : {{ ๋ณ€์ˆ˜|์˜ต์…˜ }}

  • ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ๋Š” ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€ํ™˜์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์˜ต์…˜์œผ๋กœ ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜์— ํŒŒ์ดํ”„(|)๋ฅผ ๋ถ™์ธ ๋’ค ์‚ฌ์šฉํ•ด์š”.
  • ์ฃผ๋กœ ๋ฌธ์ž์—ด์ด๋‚˜ ๋ฆฌ์ŠคํŠธ ๋“ฑ์˜ ๊ธธ์ด๊ตฌํ•˜๊ธฐ, ์Šฌ๋ผ์ด์‹ฑ, ํ•ฉ์น˜๊ธฐ ๋“ฑ์— ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ๊ธธ์ด ๊ตฌํ•˜๊ธฐ : ๐Ÿ” {{ ํ…œํ”Œ๋ฆฟ๋ณ€์ˆ˜ | length }}
    • ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฌธ์ž์—ด๋กœ ํ•ฉ์น˜๊ธฐ : ๐Ÿ” {{ ๋ณ€์ˆ˜|join:"์˜ต์…˜" }}
    • ์†Œ๋ฌธ์ž & ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ : ๐Ÿ” {{ ๋ณ€์ˆ˜|lower }} / ๐Ÿ” {{ ๋ณ€์ˆ˜|upper }}
    • ๋ฆฌ์ŠคํŠธ์—์„œ randomํ•˜๊ฒŒ ์ถ”์ถœ : ๐Ÿ” {{ ๋ฆฌ์ŠคํŠธํ˜• ๋ณ€์ˆ˜|random }}
    • ๋ฌธ์ž์—ด๋กœ ์Šฌ๋ผ์ด์‹ฑ : ๐Ÿ” {{ ๋ณ€์ˆ˜|slice:"n:n" }}
  • Django Templates์—์„œ lorem ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์•„์š”:)
    • ๐Ÿ” {{ lorem [count][mehod][random] }}
{%load static%}
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
  </head>
  <body>
    <h1>Template Language!</h1>
    {% with value='hello Django'%}
    <h4>{{value}}</h4>
    {% endwith %}
    {% lorem %}    <!-- ๐Ÿ‘ˆ lorem ์ƒ์„ฑ -->
	{% lorem 3 p%} <!-- ๐Ÿ‘ˆ lorem ๋ฌธ์žฅ(p)์„ 3๊ฐœ ์ƒ์„ฑ -->
    {% lorem 2 w random %} {% lorem 5 w random %} <!-- ๐Ÿ‘ˆ lorem ๋‹จ์–ด(w)์„ 5๊ฐœ ๋žœ๋ค ์ƒ์„ฑ -->
  </body>
</html>
profile
Keep Going, Keep Coding!

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