Django 입문 Blog – 3. Template

Template

template를 구성하기 위해 프로젝트 최상위에 있는 templates 폴더를 우리가 만든 blogapp에 이동시켜준다

template html

blogapp > templates 를 오른쪽 클릭

-> New -> HTML FILE 클릭

index.html 파일을 만들어준다

html body 내에 글을 입력해주고

view.py urls.py

templates에 만든 index.html을 보여주기 위해 view.py와 urls.py를 수정한다

view.py

view.py를 연다

다음과 같이 코드를 작성하여 index.html을 리턴해준다.

현제 아무런 처리를 하지 않고 render를 이용해 html 명을 리턴해주었다

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

urls.py

view.py에서 만든 index 함수를 맵핑해준다

urls.py를 열어주고

urlpatterns 변수 끝에 다음과 같이 추가시켜준다

path('', blogapp.views.index, name='index'),

에러가 발생하는데 blogapp view를 import하지 않았기 때문이다

다음 코드를 작성하여 포함시켜준다

import blogapp.views

이제 상단 debug나 run 버튼을 눌러 서버를 실행해준다

실행이 되면 다음과 같이 주소와 함께 콘솔창에 출력이 되니 주소를 클릭해 사이트가 잘 작동했는지 확인한다

Leave a Comment