11. 미니 프로젝트 template 설정 - eungyukm/DjangoBase GitHub Wiki

미니 프로젝트 template 설정

  1. 각 app에 templates 폴더를 각각 생성합니다.
  1. 첫 페이지 요청을 처리합니다.
  • main_app/view.py - index 함수를 작성합니다.
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader

# Create your views here.
def index(request):
    template = loader.get_template('index.html')
    return HttpResponse(template.render())
  • main_app/urls.py - index 함수에 대한 주소 매칭 코드를 작성합니다.
from django.urls import path
from . import views

urlpatterns = [
    # 주소만 입력했을 경우 (main 페이지)
    path('', views.index, name='index'),
]
  1. 상단 메뉴, 하단 정보 같은 공통 영역의 html 파일을 넣어둘 폴더를 생성합니다.
  • 프로젝트 폴더에 templates 폴더를 만들어줍니다.
  • mini_project/templates.
import os

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'mini_project', 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
  1. mini_project/settings.py 파일에 mini_project/templates 폴더를 등록해 줍니다.
    mini_project/templates 폴더에 top_menu.htnl, buttom_info.html 파일을 생성합니다.

  2. mini_project/templates 폴더에 top_menu.htnl, bottom.html 파일을 생성합니다.

  3. main_app/templates/index.html에서 상단 메뉴 부분과 하단 정보 부분을 잘라내어.
    mini_project/templates/top_menu.html과 bottom_info.html에 각각 넣어 줍니다.
    https://github.com/eungyukm/DjangoBase/blob/main/Docs/Mini_Project/Mini_Project_07.png

  4. html 파일에 상단 메뉴 부분과 하단 정보 부분에 html을 include 해 줍니다.
    {% include 'top_menu.html' %}.
    {% include 'bottom_info.html' %}.
    https://github.com/eungyukm/DjangoBase/blob/main/Docs/Mini_Project/Mini_Project_06.png