사용자 정의django 템플릿의tags와 Filters
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
polls 구조는 다음과 같다.templatetags를 어떻게 사용자 정의할 것인가
polls/
__init__.py
models.py
templatetags/
__init__.py
poll_extras.py
views.py
1,polls 설치
INSTALLED_APPS = [
.....
'polls'
]
새 templatetags
polls 디렉터리에 templatetags 디렉터리를 새로 만들고 디렉터리에 빈 파일을 생성합니다init__.py는python이 가방임을 식별하도록 합니다
1. 맞춤형 필터
from django import templateregister = template.Library()
@register.filter(name='cut')
def cut(value, arg):
return value.replace(arg, '')
@register.filter
def lower(value):
return value.lower()
2.tag
takes 열기context, 현재 상하문에 접근할 수 있습니다 context
import datetime
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def current_time(context, format_string):#context
timezone = context['timezone']
return your_get_current_time_method(timezone, format_string)
3.Inclusion tags
렌더링된 템플릿을 페이지로 직접 가져오기
#results.html
-
{% for choice in choices %}
- {{ choice }} {% endfor %}
@register.inclusion_tag('results.html')
def show_results(poll):
choices = poll.choice_set.all()
return {'choices': choices}
{% show_results poll %} //
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
사용자 정의django 템플릿의tags와 FiltersCustom template tags and filters https://docs.djangoproject.com/en/dev/howto/custom-template-tags/ polls 구조는 다음과 같다.temp...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.