简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM