● Django Template Langauge (DTL)

1. 변수

▶유용한 변수

{{ request.GET.파라미터명 }} : get방식 파라미터값 호출

{{ param }} : url ? 뒤의 쿼리 출력

{{ object.get_컬럼명_display }} : 해당 컬럼이 choice일때 value값 출력

{{ request.path }} : url (without GET parameters)

{{ request.get_full_path }} : url (with GET parameters)

{% for shop in shop_list %}
  <tr>
    <td>{{ shop.id }}</td>
    <td>{{ shop.get_category_display }}</td>
  </tr>
{% endfor %}

 

 

3. 필터

 - default

{{ value | default:'nothing' }}

 - length : value의 길이

 

 - intcomma : 3자리 구분 콤마 삽입

① settings.py

INSTALLED_APPS = [
    ...
    'django.contrib.humanize',
    ...
]

② template.html

{* load humanize *}
...
...
{* object.name|intcomma *} 

 

 

● Template 확장 (= 상속)

공통된 Base 템플릿을 바탕으로 변경되는 Template을 확장시키는 방식

- 형식 : 

{% block 블럭명 %}

{% endblock 블럭명 %}

<!-- polls/template/base.html -->

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% include 'base/header.html' %}
    
    {% block content %}
    {% endblock content %}

    {% include 'base/footer.html' %}
</body>
</html>
<!-- index.html -->

{% extends 'base.html' %}
 
{% block content %}
    <h1>{{message}}</h1>
{% endblock content %}

→ extends 경로는 프로젝트명/templates 이하부터 적음

 

 

 

 

 

'Framework > Django' 카테고리의 다른 글

사용자 정보 가져오기(user)  (0) 2020.12.14
Django Form(장고 폼)  (0) 2020.12.10
MTV 패턴 - View  (0) 2020.12.09
MTV 패턴 - Model  (0) 2020.12.09
generic view(지네릭 뷰) - Class Based View(CBV, 클래스뷰)  (0) 2020.12.09

+ Recent posts