Django: can't pass variable to included template?

I had a problem when I want to use a template, including Django.

Here is a real example: I got 3 files:

  • home.html (get the context variable passed from Views )
  • base.html (skeleton template file)
  • and header.html (included in base.html).

If I put the code below directly in base.html without including header.html , the {{title}} variable passed from the house is correctly called. But if I include header.html in base.html , the value of the {{title}} variable cannot be called.

<title>{% block title %}{% endblock %} | {{ SITE_INFO_TITLE }}</title>

Is there any solution to this problem? Thank.

+5
source share
3 answers

As far as I know, blocks and variables are different in django. If you want to pass the name as a context variable, you must set it using the declaration in base.html, for example:

{% include "header.html"%} 

Which, in turn, contains:

{% block title %} {{title}} {%endblock%}

You can also install it at home. {% block title%} Homepage {% endblock%} But I'm also trying to set in the context of the template. Without title block.

def test_view(ctx):
  xa = { "title":"Sommaire"}
  return render_to_response("test.html",xa)

I think you can also see the template tag with, I think you can set the context variable using this tag.

+3
source

{% include %}? : https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#include

{% include "name_snippet.html" with person="Jane" greeting="Hello" %}
+8

, Django. "child" "parent".

, . , . , , .

+4

All Articles