Web/Django

Django 템플릿(3) 명령어

_data 2023. 3. 5. 16:23

템플릿(2)에 이어서 for문, if,elif, else문을 작성할 수 있다.

 

1. variables.html에 아래의 코드를 입력한다.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Variables!</h1>
    <p>In korea, the most best player ST is {{ST | lower}} , MF is {{MF}}, DF is {{DF}}.</p>
   
    <p>Son's height is {{height.0}}. </p>
   
    <p>their heights are 
    {% for item in height%}
        <p>{{item}}</p>
        {% endfor %}
    </p>

</body>
</html>


for 문을 쓸 때 아래와 같은 Syntax로 사용한다.

{% for item in items %}
	{{item}}
{% endfor %}

if문은 아래와 같은 Syntax로 사용한다.

{% if 변수명 == 1 %}
	Welcome admin!

{% else %}
	Welcome User!
	
{% endif %}

== , != , 'in' 'is' 도 사용할 수 있다.