I need to know how to get this code working on Django.
This example WORKS:
View:
def index(request):
if request.user.is_authenticated():
username = request.user.username
else:
username = None
Template:
{{ username }}
Now what I want to do is this, but this is NOT WORKING:
View:
def index(request):
username = request.user.username
Template:
{% if user.is_authenticated %}
{{ username }}
{% endif %}
Use the template in this way is possible? I am a beginner, I am just testing how things work here.
Any clue?
Best Regards,
Note that if you pass
RequestContextto your templates (which is true most of the time), you already haverequest.useraliased simply as{{ user }}. Then you do just{{ user.username }}and the view function needs to know nothing about this.