1. Django Tutorial(Airbnb) - Template Filter

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

Django

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

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” Django์— "Built-in Template Filter"๊ณผ "Custom Template Filter"๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.



๐ŸŒˆ Custom Template Filter

๐Ÿ”ฅ Built-in Template Filter

๐Ÿ”ฅ Custom Template Filter



1. Built-in Template Filter

Django๋Š” Template Filter๋ฅผ ํ†ตํ•ด Template์—์„œ ํŒŒ์ดํ”„(|)๋กœ ์‹œ์ž‘ํ•ด์„œ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋ฌธ์ž์—ด ๋“ฑ์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ๋„์›€์„ ์ค๋‹ˆ๋‹ค.
์•„๋ž˜ ์†Œ๊ฐœ๋œ "Built-in Template Filter" ๋ฟ๋งŒ์•„๋‹ˆ๋ผ |date, |addslashes, |cut, |floatformat ๋“ฑ ๋‹ค์–‘ํ•œ Template Filter๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”Ž |add : ์ˆซ์ž๋ฅผ ๋”ํ•ด์ค๋‹ˆ๋‹ค.

{{ value|add:"2" }}  # ๐Ÿ‘ˆ value = 3 ์ด๋ผ๋ฉด,, 2๋ฅผ ๋”ํ•ด 5๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

๐Ÿ”Ž |capfirst : ์ฒซ๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ์‹œ์ผœ์ค๋‹ˆ๋‹ค.

{{ value|capfirst }} # ๐Ÿ‘ˆ value = 'django' ๋ผ๋ฉด,, "Django"๋กœ ๋ณ€ํ™˜

๐Ÿ”Ž |center : ๊ณต๋ฐฑ์„ ์ฃผ๊ณ , ๊ฐ€์šด๋ฐ ์ •๋ ฌ์„ ํ•ด์ค˜์š”.

"{{ value|center:"15" }}" # ๐Ÿ‘ˆ value = 'Django' ๋ผ๋ฉด,, "    Django    "๋กœ ๋ณ€ํ™˜

๐Ÿ”Ž |first : ๋ฆฌ์ŠคํŠธ์—์„œ ๋งจ ์ฒ˜์Œ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

{{ value|first }} # ๐Ÿ‘ˆ value = ['a', 'b', 'c']๋ผ๋ฉด,, 'a'๋งŒ ๊ฐ€์ ธ์˜ด

๐Ÿ”Ž |join : python join ๋ฌธ๋ฒ•๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์ด์—์š”.

{{ value|join:" // " }} # ๐Ÿ‘ˆ value = ['a', 'b', 'c']๋ผ๋ฉด,, "a // b // c"

2. Custom Template Filter


๐Ÿค” Custom Template Filter ์ƒ์„ฑํ•˜๊ธฐ

ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์ด "Built-in Template Filter"๋กœ ์ œ๊ณต๋˜์ง€ ์•Š๋‹ค๋ฉด, ์ง์ ‘ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ "Custom Template Filter"๋ผ ํ•ฉ๋‹ˆ๋‹ค.

str์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ์‹œํ‚ค๋Š” Custom Template Filter๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. "Custom Template Filter"๋ฅผ ์‚ฌ์šฉํ•  app ๋””๋ ‰ํ† ๋ฆฌ ๋ฐ”๋กœ ์•„๋ž˜์— "templatetags" ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ƒ์„ฑํ•˜๋ฉด Django๊ฐ€ ์ธ์‹ํ•  ์ˆ˜ ์—†์œผ๋‹ˆ ๊ผญ "templatetags"๋ผ๋Š” ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
"templatetags" ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์— "__init__.py"์™€ ๋“ฑ๋กํ•  ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์ค„ python ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” "my_filter.py"๋กœ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

app dir/ # ๐Ÿ‘ˆ ์‚ฌ์šฉํ•  app ๋””๋ ‰ํ† ๋ฆฌ
    __init__.py
    models.py
    templatetags/ # ๐Ÿ‘ˆ "templatetags" dir ๋งŒ๋“ญ๋‹ˆ๋‹ค:)
        __init__.py       # ๐Ÿ‘ˆ __ini__.py๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค:)
        my_filter.py      # ๐Ÿ‘ˆ python ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ํŒŒ์ผ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค:)
    views.py

Custom Template Filter๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜ 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋ฐฉ๋ฒ•1 : ๋ฐ์ฝ”๋ ˆ์ด์…˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์œผ๋กœ name๊ฐ’์— ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
from django import template       # ๐Ÿ‘ˆ "template" import
register = template.Library()     # ๐Ÿ‘ˆ template.Library()์— ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
@register.filter(name="my_upper") # ๐Ÿ‘ˆ template.Library()์— filter๋ฅผ ๋“ฑ๋ก
def my_upper(value):
    return value.upper()
  • ๋ฐฉ๋ฒ•2 : filter์˜ ์ฒซ๋ฒˆ์งธ argument๋กœ template์—์„œ ์‚ฌ์šฉํ•  ์ด๋ฆ„์„ str๋กœ ๋„ฃ๊ณ , ๋‘๋ฒˆ์งธ argument๋กœ ํ•จ์ˆ˜๋ฅผ ๋ช…์‹œํ•ด์ค๋‹ˆ๋‹ค.
from django import template # ๐Ÿ‘ˆ "template" import
register = template.Library() # ๐Ÿ‘ˆ template.Library()์— ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
def my_upper(value):
    return value.upper()
register.filter("lululala", my_upper) # ๐Ÿ‘ˆ template์—์„œ |lululala๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์š”:)

๐Ÿค” Custom Template Filter ์‚ฌ์šฉํ•˜๊ธฐ

  • ์‚ฌ์šฉํ•  ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์ด๋™ํ•ด ์ƒ๋‹จ ๋ถ€๋ถ„์— "my_filter.py"๋ฅผ load๋“œ ํ•ฉ๋‹ˆ๋‹ค.
{% extends "base.html" %}
{% load my_filter %}  # ๐Ÿ‘ˆ custom template filter๋ฅผ load
{% block content %}
    {{'somthing'|my_upper}} # ๐Ÿ‘ˆ ๋“ฑ๋กํ•œ filte ๊ธฐ๋Šฅ์ด ์ ์šฉ๋˜์–ด 'somthing'์ด 'SOMETHING'๋กœ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.
{% endblock %}
profile
Keep Going, Keep Coding!

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