Backend/Django

django 03. 장고 template의 extends, include 구문과 render 함수

meong_j 2021. 10. 5. 18:33
728x90
반응형

1. 장고에서 extends와 include 차이

- extends : 미리 만들어 놓은 html파일을 가져와서 이것을 바탕으로 template 채워나는 구조

- include : 조그만 조각같은 것을 가져와 붙이는 개념

 

 

2. templates 폴더 만들고 html 파일 안에 생성

{% extends 'base.html' %}
<!-- base.html 템플릿를 기반으로 안에 내용만 바꿔줌-->
{% block content %}

    <div style="border-radius: 1rem; margin: 2rem; text-align: center">
        <form action="/account/hello_world/" method="post">
            {% csrf_token %}
            <div>
             <input type="submit" class="btn btn-primary" value="POST">
            </div>
        </form>

{% endblock %}
  • hello_world.html 파일 생성
  • main App에서 작성한 base.html 로 뼈대 구성 후 extends로 안에 내용만 수정
  • account > views.py 에 html 등록

 

3. settings.py 에 TEMPLATES안에 생성한 template 등록

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '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',
            ],
        },
    },
]

4. 소스 추가 한 것  git commit

반응형