본문 바로가기

Python_WEB/Try_Django

[Django]Render Context in Templates

반응형

CodingEntrepreneurs Django 강의 정리

format 출력 차이점>

1. Python 일반적 format

my_title = "Title TEST"
doc = "<h1>{title}</h1>".format(title=my_title)

print(doc)

'<h1>Title TEST</h1>'

 

2. Django

my_title = "Title TEST"
django_render_doc = "<h1>{{ title }}</h1>".format(title=my_title)

Title TEST

 

views>

from django.http import HttpResponse
from django.shortcuts import render

# Model View Template (MVT)
def home_page(request):
    my_title = "Hello there ..."
    return render(request, "hello_world.html", {"title": my_title})


def about_page(request):
    return render(request, "hello_world.html", {"title": "About Us"})


def contact_page(request):
    return render(request, "hello_world.html", {"title": "Contact Us"})

 

hello_world.html>

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
        integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

    <title>Hello, world!</title>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-6 mx-auto">
                <h1>{{ title }}</h1>
            </div>
        </div>
    </div>


    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
        integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
        integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
        crossorigin="anonymous"></script>
</body>

</html>

 

적용 스크린샷>

반응형

'Python_WEB > Try_Django' 카테고리의 다른 글

[Django]Rendering Any Kind of Template  (0) 2020.06.14
[Django]Stay DRY with Templates  (0) 2020.06.14
[Django]Add Bootstrap  (0) 2020.06.13
[Django]Loading a HTML Template  (0) 2020.06.13
[Django]Your First Template  (0) 2020.06.13