06. Template - eungyukm/DjangoBase GitHub Wiki

Template

  1. 동적 웹 페이지 구성을 위해 Template 개념을 활용합니다.
    indexTemplate = loader.get_template('Index.html')
    return HttpResponse(indexTemplate.render())
  1. 주석
<!DOCTYPE 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>
    {# 이 부분은 주석입니다. #}

    {% comment %}
        이 부분은
        여러 줄
        주석입니다.
    {% endcomment %}

</body>
</html>
  1. views.py Code
    indexTemplate = loader.get_template('Index.html')
    title = "안녕하세요 Django 입니다"
    render_data = {
        'title' : title
    }

    return HttpResponse(indexTemplate.render(render_data, request))
  1. 변수 출력
<!DOCTYPE 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>
    {# 이 부분은 주석입니다. #}

    {% comment %}
        이 부분은
        여러 줄
        주석입니다.
    {% endcomment %}

    <h1>{{title}}</h1>

    {% 변수 출력 %}
    {% with discription="반갑습니다" %} 
    <h3>{{discription}}</h3>
    {% endwith %}
</body>
</html>
  1. if-else문
<!DOCTYPE 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>
    {# 이 부분은 주석입니다. #}

    {% comment %}
        이 부분은
        여러 줄
        주석입니다.
    {% endcomment %}

    <h1>{{title}}</h1>

    {# 변수 출력 #}
    {% with discription="반갑습니다" %} 
    <h3>{{discription}}</h3>
    {% endwith %}

    {% if a1 == 10 %} 
    <h3>h1은 10입니다.</h3>
    {% elif a1 == 5 %} 
    <h3>a1은 5입니다</h3>
    {% elif a1 == 10 %}
    <h3>a1은 10압니다.</h3>
    {% else %} 
    <h3>a1은 20이 아닙니다</h3>
    {% endif %}
</body>
</html>
  1. for문 및 cycle
<!DOCTYPE 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>
    {# 이 부분은 주석입니다. #}

    {% comment %}
        이 부분은
        여러 줄
        주석입니다.
    {% endcomment %}

    {% comment %}
    <h1>{{title}}</h1>
    {% endcomment %}

    {# 변수 출력 #}
    {% with discription="반갑습니다" %} 
    <h3>{{discription}}</h3>
    {% endwith %}

    {% if a1 == 10 %} 
    <h3>h1은 10입니다.</h3>
    {% elif a1 == 5 %} 
    <h3>a1은 5입니다</h3>
    {% elif a1 == 10 %}
    <h3>a1은 10압니다.</h3>
    {% else %} 
    <h3>a1은 20이 아닙니다</h3>
    {% endif %}

    <hr/>
    {% for m in members %} 
    {{m.id}}<br/>
    {{m.firstname}}<br/>
    {{m.lastname}}<br/>
    <hr/>
    {% endfor %}

    {% for m in members%}
    <div>
    {{m.id}}><br/>
    {{m.firstname}}<br/>
    {{m.lastname}}<br/>
    </dvie>
    <hr/>
    {% endfor%}
</body>
</html>
⚠️ **GitHub.com Fallback** ⚠️