Language/Django

Django에서 index.html 연결하기

웜듀 2021. 6. 13. 20:56

template 및 index.html 생성

helloworld 앱 안에 template 디렉토리를 하나 생성해주고 그 안에 helloworld 디렉토리를 하나 만들어 준 뒤 그 helloworld 디렉토리 안에 index.html을 만든다

즉, helloworld/template/helloworld/index.html 순서

그 후 html 내용을 작성한다

html 연결을 위한 urls.py, views.py

testproject/urls.py와 helloworld/views.py를 다음과 같이 수정해준다

# views.py

from django.shortcuts import render

# Create your views here.
def index(request):
    return render(request, 'helloworld/index.html')
#urls.py

from django.contrib import admin
from django.urls import path
from helloworld import views # helloworld에서 views 함수를 가져온다

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index), # url에 아무것도 입력하지 않았을 때의 경로를 main함수와 연결
]

 

urlpattern 작성법

path(경로, view에 정의된 함수, 이름)