Python/초간단 장고 Django

django {% with %}

컴닥 2022. 9. 7. 23:17
반응형

https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#with

 

Built-in 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

Caches a complex variable under a simpler name. 
복잡한 변수를 더 간단한 이름으로 캐시합니다. 

This is useful when accessing an “expensive” method (e.g., one that hits the database) multiple times.
이것은 "비싼" 방법(예: 데이터베이스를 조회하는 것)에 여러 번 액세스할 때 유용합니다.

 

{% with total=business.employees.count %}
    {{ total }} employee{{ total|pluralize }}
{% endwith %}

The populated variable (in the example above, total) is only available between the {% with %} and {% endwith %} tags.
캐시된(옮겨진) 변수(위의 예에서는 total)는 {% with %} 태그와 {% endwith %} 태그 사이에만 사용할 수 있습니다.

 

You can assign more than one context variable:
둘 이상의 컨텍스트 변수를 할당할 수도 있습니다.

{% with alpha=1 beta=2 %}
    ...
{% endwith %}

 

Note: The previous more verbose format is still supported. 
이전 장황한(verbose) 형식은 여전히 지원됩니다.

{% with business.employees.count as total %}
반응형