alter123
alter123

Reputation: 601

How to pass parameter from view to template in django?

I want to pass value of variable from django view to template.

view:

def countsnippet(request):
 checkins = Attendee.objects.filter(checkin=True).count()
 return render( request, 'search/countsnippet.html', {'inft': checkins} )

Template snippet:

<div class="row" >
  {% block content %}
  <p>INFT : {% 'inft' %}</p>
  {% endblock %}
</div>

I want to pass checkins value to template, but using above approach is giving me error.

Upvotes: 1

Views: 144

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476547

You render a "variable" by placing it between double curly brackets ({{ }}), like:

<div class="row" >
  {% block content %}
  <p>INFT : {{ inft }}</p>
  {% endblock %}
</div>

The variable name is not surrounded by quotes, and you can treat it thus like an identifier.

You can furthermore use a sequence of identifiers separated by dots (like {{ foo.bar }}) to access the bar attribute/element of foo.

Furthermore Django templates allow extra features like template filters, etc. See the Django documentation on the template language for an in-depth article.

Upvotes: 1

Related Questions