๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„/WIL(Week I Learned)

WIL_220612_ํŒ€ ํ”„๋กœ์ ํŠธ Django

  • -
 

๋ฐฐ์šด ๋‚ด์šฉ

 


 

์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜ ์„ค์ •

 

์ถœ์ฒ˜ : http://daplus.net/python-django-%ED%85%9C%ED%94%8C%EB%A6%BF-%EB%82%B4%EC%97%90%EC%84%9C-%EC%9D%B8%EB%8D%B1%EC%8A%A4%EB%A1%9C-%EB%AA%A9%EB%A1%9D-%ED%95%AD%EB%AA%A9%EC%9D%84-%EC%B0%B8%EC%A1%B0%ED%95%A9%EB%8B%88/

 

[python] Django ํ…œํ”Œ๋ฆฟ ๋‚ด์—์„œ ์ธ๋ฑ์Šค๋กœ ๋ชฉ๋ก ํ•ญ๋ชฉ์„ ์ฐธ์กฐํ•ฉ๋‹ˆ๊นŒ? - ๋ฆฌ๋ทฐ๋‚˜๋ผ

์ด๊ฒƒ์€ ๊ฐ„๋‹จ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ฃผ์œ„๋ฅผ ๋‘˜๋Ÿฌ ๋ณด์•˜์ง€๋งŒ ๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. Django ํ…œํ”Œ๋ฆฟ์˜ ๋ชฉ๋ก์—์„œ ๋‹จ์ผ ํ•ญ๋ชฉ์„ ์ฐธ์กฐํ•˜๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ์ฆ‰ {{ data[0] }}, ํ…œํ”Œ๋ฆฟ ์–ธ์–ด ๋‚ด์—์„œ ํ•ด๋‹น

daplus.net

์ถ”๊ฐ€ ์ •๋ณด ) ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ : https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

 

How to create custom template tags and filters | Django documentation | Django

Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate

docs.djangoproject.com

 

 

- ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋ฐ›์•„์˜ฌ ์ •๋ณด๊ฐ€ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ถˆ๋Ÿฌ์™€์ง€๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ ์ •๋ณด๋ฅผ ๋‚˜์—ดํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

# templatetags / index.py
from django import template
register = template.Library()

@register.filter
def index(indexable, i):
    return indexable[i]

 

- ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ settings.py ๋ฅผ ์„ค์ •ํ–ˆ๋‹ค.

- libraries : index: templatetags.index ์ถ”๊ฐ€

TEMPLATES = [
	{
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        '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',
            ],
        'libraries':{
            'index': 'templatetags.index',
            }
        }
    }
]

 

- django ํ…œํ”Œ๋ฆฟ์— {% load index %} ๊ตฌ๋ฌธ์„ ์ถ”๊ฐ€

- ์˜ˆ๋กœ my_list [x] ๊ฐ€ ์žˆ๋‹ค๋ฉด {{ my_list|index:x }} ์œผ๋กœ ์‚ฌ์šฉํ•  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ€์ž…

 

- ์œ„์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ–ˆ๋˜ ์ด์œ ๋Š” ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ํฌ๋กค๋ง ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ๋ฐ›์•„์™€์ ธ์„œ ๊ทธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ์œผ๋‚˜, ํฌ๋กค๋ง ๋œ ๋ฐ์ดํ„ฐ๋ฅผ db ์— ์ €์žฅํ•˜์—ฌ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฒดํƒํ–ˆ๊ณ , ์œ„์˜ ๋ฐฉ๋ฒ•์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ๋กœ ํ–ˆ๋‹ค.

 


 

django CSS ์ ์šฉ

 

- ์›์ธ : ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ style.css ํŒŒ์ผ์— ์ž„ํฌํŠธํ•˜์—ฌ ์‚ฌ์šฉํ•˜์˜€๋‹ค. html ์— link ๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, ์ ์šฉ๋˜์ง€ ์•Š์•˜๋‹ค.

CSS ๋ถ„ํ• 

- ํ•ด๊ฒฐ : ๋ธŒ๋ผ์šฐ์ €์˜ ์บ์‹œ ๋ฌธ์ œ์˜€๋‹ค. ์บ์‹œ๋ฅผ ์ •๋ฆฌํ•˜๊ณ  ๋‚˜๋‹ˆ ์ž˜ ์ ์šฉ๋˜์—ˆ๋‹ค. ๋‹ค์Œ ์‚ฌ์šฉํ•  ๋•Œ๋„ ์ƒˆ๋กœ์šด css ์ž„ํฌํŠธ๊ฐ€ ๋˜์—ˆ๋‹ค๋ฉด, ์บ์‹œ๋ฅผ ํ•œ ๋ฒˆ ์ •๋ฆฌํ•˜๊ณ  ์ƒˆ๋กœ๊ณ ์นจ์„ ํ•˜๊ฑฐ๋‚˜ ์บ์‹œ๊ฐ€ ๋ˆ„์ ๋˜์ง€ ์•Š๊ฒŒ ์‹œํฌ๋ฆฟ(ํผ๋ธ”๋ฆญ) ๋ชจ๋“œ๋ฅผ ์ด์šฉํ•˜์ž.

'๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ > WIL(Week I Learned)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

WIL_220508  (0) 2023.01.01
WIL_220501  (0) 2023.01.01
WIL_220424  (0) 2023.01.01
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.