본문 바로가기

Python_WEB/Project-Kindergarten

Project Code Review>Link to html File

반응형

이제는 HTML 파일을 만들어서 views에서 어떻히 연결하는지 확인을 해본다.

 

blog > views.py

from django.shortcuts import render


def index(request):
    context = {

    }

    return render(request, 'index.html', context=context)

또는

from django.shortcuts import render


def index(request):
    return render(request, 'index.html', {})
index.html 을 불러온후 context의 내용을 전달한다.
context 템플릿에서 쓰이는 변수명과 Python 객체를 연결하는 사전형 값
render(request 객체, 템플릿 이름, context 사전형 객체)

 

blog > templates > index.html(생성)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Hello World</title>
  </head>
  <body>
    <h1>Hello World</h1>
  </body>
</html>

 

blog 폴더 구조

blog/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    templates/
        index.html
    tests.py
    urls.py
    views.py

 

개발서버 확인

python manage.py runserver

System check identified no issues (0 silenced). 
May 30, 2020 - 08:32:08 
Django version 3.0.6, using settings 'config.settings' 
Starting development server at http://127.0.0.1:8000/ 
Quit the server with CTRL-BREAK. 
[30/May/2020 08:32:11] "GET / HTTP/1.1" 200 20

 

브라우저 확인

반응형