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** ⚠️