Django

Django(hello>hello02)[2]

FnMask 2020. 11. 21. 15:07

[1]에서

Hello,django template!!

화면이 잘 나왔으면 다음 단계로 넘어가겠습니다.

templates>variable01을 만들어주세요

    <h1>{{lst.0}}</h1>	#list의 0번지와
    <p>{{lst.1}}</p>	#list의 1번지를 가지고 올겁니다.

templates와 연결할 것이기 때문에 hello02>views.py로 이동합니다.

 

views.py

from django.shortcuts import render

# Create your views here.
def hello(request):
    return render(request, 'hello02.html', {'name': 'django template'})

def variable01(request):
    lst = ['python', 'django']
    return render(request, 'variable01.html', {'lst': lst})

 데이터구성을 완료했습니다. 연결을 해 주기 위해 hello02>urls.py로 이동합니다.

 

urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('', views.hello),
    path('var01', views.variable01),
]

실행시 화면입니다.

잘 뜨네요

-----------------------------------------------------------------------------------------------------------------------------------

 

hello>template>variable02.html을 만들어줍니다.

 

hello>template>variable02.html

html:5 (엔터치면 vs코드 자동완성됩니다.)
    <h1>{{dct.class}}반 {{dct.name}}님 환영합니다.</h1>

variable02에 값을 전달해줄 hello02/views.py로 갑니다.

 

hello>hello02>views.py

from django.shortcuts import render

# Create your views here.
def hello(request):
    return render(request, 'hello02.html', {'name': 'django template'})

def variable01(request):
    lst = ['python', 'django']
    return render(request, 'variable01.html', {'lst': lst})

def variable02(request):
    dct = {'class': 'qclass', 'name': '박보검'}
    return render(request, 'variable02.html', {'dct': dct})

hello>hello02>views.py와 연결해줄 hello>hello02>urls.py 로 이동합니다.

hello>hello02>urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('', views.hello),
    path('var01', views.variable01),
    path('var02', views.variable02),	#var02로 요청이 들어오면 views.py가 가진 variable02와 연결
]

hello02/var02 로 실행하면 정상작동됩니다.

완성