Hoa
Hoa

Reputation: 20438

In Django, how can I access a parameter in a template?

From my view I'm doing the following:

return render(request, 'yabe/login.html', {'error': True})

In my template I'm trying

{% if error %}
    <div class="error">Authentication error. Please try again</div> 
{% endif %}

But it's not working

Upvotes: 0

Views: 45

Answers (1)

santiagobasulto
santiagobasulto

Reputation: 11686

If you're using django.shortcuts.render that should work. The problem you might have could be some ContextManager overriding that context variable. Try this:

Your view:

from django.shortcuts import render

def your_view(request):
    ...
    return render(request, 'yabe/login.html', {'errorUsedJustHere': True})

Your template:

{% if errorUsedJustHere %}
    <div class="error">Authentication error. Please try again</div> 
{% endif %}

Extra. You could use Django Debug Toolbar to see what variables are set in the context.

Upvotes: 1

Related Questions