brunch

매거진 백엔드

You can make anything
by writing

C.S.Lewis

by 내가 사는 세상 Jan 17. 2024

Django - Templates

목차

1. 뼈대 만들기 #common.html

2. 살 붙이기 # {% block content %} {% endblock %}

3. 끼워넣기 # {% include 'app_name/xxx.html' %}






1. 뼈대 만들기 #common.html


templates/common.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
</head>


<body>
{% block content %}
{% endblock %}

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
        crossorigin="anonymous"></script>
</body>
</html>




2. 살 붙이기 # {% block content %} {% endblock %}




3. 끼워넣기 # {% include 'app_name/xxx.html' %}


with절 사용가능


{% include 'portfolio/search_bar.html' %}



또 다른 예시


<!-- parent_template.html -->

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Parent Template</title>

</head>

<body>

    <div>

        <!-- Include 구문과 함께 with 절 사용 -->

        {% include 'child_template.html' with message="Hello from parent template!" %}

    </div>

</body>

</html>



<!-- child_template.html -->

<div>

    <p>{{ message }}</p>

</div>

매거진의 이전글 Django - 로그인, 로그아웃 기능
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari