728x90

python manage.py startapp hello02


settings.py에 54번째줄을 보면 TEMPLATES가 있습니다.
여기 DIRS의 배열에 [BASE_DRI/'templates']를 적어주세요

hello아래 hello02.html파일을 만들고 해달 내용을 작성했습니다.
파일을 만들고 html5를 누르면 템플릿이 자동으로 완성됩니다.
<body></body>에
<h1>Hello, {{name}}!!</h1>
만 작성해주세요.
from django.shortcuts import render
# Create your views here.
def hello(request):
return render(request, 'hello02.html', {'name':'django'})

hello02폴더에 있는 views에 해당 코드를 작성합니다.
from django.urls import path
from . import views
urlpatterns = [
path('', views.hello),
]

path('hello02/', include('hello02.urls')),

def index(request):
return HttpResponse('''<h1>hello, world!</h1>
<a href="/hello01">hello01</a><br/>
<a href="/hello02">hello02</a>
''')



path('var01', views.variable01),

def variable01(request):
lst = ['Python', 'Django', 'Templates']
return render(request, 'variable01.html', {'lst': lst})

<!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>
<h1>{{lst.0}}</h1>
<p>{{lst.1}}</p>
<p>{{lst.2}}</p>
</body>
</html>

<a href="var01">var01</a>
<a href="var02">var02</a>


def variable02(request):
dct = {'class': 'qclass', 'name': '자기이름'}
return render(request, 'variable02.html', {'dct': dct})

path('var02', views.variable02),



728x90
'Python > Django' 카테고리의 다른 글
[Django] 장고와 sqlite3로 CRUD 구현하기 (0) | 2022.05.19 |
---|---|
[Django] 장고 이용하여 페이지 구성하기 (0) | 2022.05.18 |
[Django] 장고 이용하여 페이지 구성하기01 (0) | 2022.05.16 |
[Django] 설치부터 기본 세팅(VSCODE) (0) | 2022.05.15 |