Django

Django(hello>hello02)[4] href

FnMask 2020. 11. 21. 15:51

hello>templates>href.html 만들어주세요

<body>
    <a href='{%url "index" %}'>go!</a>
</body>

 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})

def forLoop(request):
    return render(request, 'for.html', {'number': range(1, 10)})

def if01(request):
    return render(request, 'if01.html', {'user': {'id': 'qclass', 'name': 'teacher'}})

def if02(request):
    return render(request, 'if02.html', {'role': 'manager'})

def href(request):
    return render(request, 'href.html')

urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('', views.hello),
    path('var01', views.variable01),
    path('var02', views.variable02),
    path('for', views.forLoop),
    path('if01', views.if01),
    path('if02', views.if02),
    path('href', views.href),
]

hello02/href 로 요청해서 go! 를 클릭하면

hello02/href

hello01의 인덱스로 갑니다.

hello01/
hello01/urls.py에 index

hello01/urls.py에 index라는 이름으로 해당 요청 경로를 등록해 놨기 때문에

hello01의 이름을 index로 지정했기 때문에 href.html에서 {% url "index" %} 로 요청했을 때 index라는 이름을 찾았기 때문입니다.