Home > Mobile >  How I can Extend templates In djnago?
How I can Extend templates In djnago?

Time:09-23

enter image description here

{% extends "index.html" %}

{% block content %}

<h1>test for the extend</h1>

{% endblock %}

extend block is not working ? please any advice

CodePudding user response:

did you past your template path like that TEMPLATES_DIR = BASE_DIR / 'templates'

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

if yes then try single cote instead of double

CodePudding user response:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{% block content %}

{% endblock %}
</body>
</html>

child.html

{% extends 'index.html' %}

{% block content %}
<h1> This is child html page </h1>
{% endblock %}

make sure to config the templates folder in settings.py

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

and also create a folder named templates in your project where manage.py is. the templates contains the two files index.html and child.html

  • Related