Send objects to a template base.html Django

2

My situation is as follows, I have a file base.html where more than one page of a extend in it. My question is, how do I send two objects to this file, so that these objects are shown in all other views, that extend the page (base.html);

    
asked by anonymous 20.07.2015 / 20:57

2 answers

3
  

My question is, how do I send two objects to this file?

Well, to send two objects to a template you can pass context of the function render

Ex:

def foo(request):
    object1 = Object1.object.all()
    object2 = Object2.object.all()

    context = {'object1': object1, 'object2': object2}

    return render(request, 'template.html', context)
  

for these objects to be shown in all other views, which   the extend in this page (base.html)

The variables / objects that you send to the template are only available for the template that was sent.

In order for the variables / objects to be available in the other templates, you must use context processor .

Ex:

views.py

def foo(request):
    object1 = Object1.object.all()
    object2 = Object2.object.all()

    context = {'object1': object1, 'object2': object2}

    return context

settings.py

TEMPLATE_CONTEXT_PROCESSORS += ("sua_app.views.foo", )
    
07.08.2015 / 18:51
2

You can do this in your view:

{% extends variavel %}

And in the project something like this:

import django.http
from django.shortcuts import render_to_response
# declarando uma constante aqui
INDEX_EXTEND = "index.html"
# e utilizando algo como isso
def response(request) :
    return render_to_response("base.html", {'variavel': INDEX_EXTEND})
    Read more here: link     
20.07.2015 / 21:51