D
D
Dmitry Vyatkin2015-12-24 07:19:38
Django
Dmitry Vyatkin, 2015-12-24 07:19:38

How to pass data between templates in django?

There is template A and template B. B is inherited from A. Templates are called by different views, data from the model is transferred to A, how to make this data also inherited? Interested in how to do it right, is it worth using ajax or caching here?
Example:

Шаблон А
{% extends "site_base.html" %}

{% load i18n %}

{% block head_title %}home{% endblock %}

{% block body %}

<div class="row">
    <a href="#">
        <img  class="img-responsive" src={{ my_image}}>
    </a>
</div>
<div class="row">
    {% for object in objects_list %}
        <div class="col-lg-2">
            <a href="#" class="">
              <img  src="{{ object.my_image }}">
            </a>
        </div>
    {% endfor %}

    {% block result %}
        <p>Hello</p>
    {% endblock %}

{% endblock %}

Шаблон Б
{% extends 'a.html' %}

{% load i18n %}

Как надо правильно подгружать картинки из шаблона А?

{% block result %}
    <p>Шаблон Б<p>
{% block endblock %}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Vyatkin, 2015-12-28
@dim137

You need to use inclusion_tag().

I
IvanOne, 2015-12-24
@IvanOne

maybe it makes sense to make an included template {%include%}? Write in more detail

R
Rostislav Grigoriev, 2015-12-24
@crazyzubr

Inherit the view, or make a separate function that returns the data needed for both views.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question