S
S
sazhyk2016-05-23 16:55:42
Django
sazhyk, 2016-05-23 16:55:42

Why are blocks not included in Django?

Help solve the problem. Templates in django don't work.
Project structure:

---project
   |--project
   |  |--settings.py
   |  |--urls.py
   |--app1
   |  |--static
   |  |  |--css
   |  |  |  |--style.css
   |  |--templates
   |  |  |--left.html
   |  |  |--right.html
   |  |--urls.py
   |  |--models.py
   |  |--views.py
   |--temlpates
   |  |--index.html
   |--manage.py

settings.py
...
INSTALLED_APPS = [
    ...
    'app1',
    ...
...
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            'templates',
            'app1/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',
            ],
        },
    },
]
...
STATIC_URL = '/static/'

views.py
from django.shortcuts import render_to_response

def index(request):
    return render_to_response('index.html')

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="main">
    {% block left %}{% endblock %}
    {% block right %}{% endblock %}
</div>
</body>
</html>

left.html
{% extends 'index.html' %}

{% block left %}
    <p>Абра-кадабра!</p>
{% endblock %}

right.html
{% extends 'index.html' %}

{% block right %}
    <p>Сим-селявим!</p>
{% endblock %}

As a result, the output in the browser is:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="main">
    
</div>
</body>
</html>

That is, the contents of the index without left and right blocks. Jungle server. Everything is fine in the console. Styles from the folder staticare loaded. I tried to transfer left.html and right.html to the folder project/temlpatesand to project/app1/templates- to no avail. What am I doing wrong? I can not understand? Perhaps I misinterpreted the template manual?
PS. sorry for the footcloth, I thought it would be clearer. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2016-05-23
@sim3x

def left(request):
    return render_to_response('left.html')

{% extends 'index.html' %}

{% block left %}
    <p>Абра-кадабра!</p>
{% endblock %}

{% block right %}
    <p>Сим-селявим!</p>
{% endblock %}

A
Alexey Ovdienko, 2016-05-23
@doubledare

If you want to see the blocks - set them in the index file inside the blocks.
Templates work a little differently. In the case of extend, they move if they are referenced by the django url, then the template looks for the parent in the specified extend and inserts the blocks.
You can try include, but the django template engine is bent in performance if there are a lot of includes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question