Django template system, how to get this property?
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
Templat开发者_运维百科e:
{% 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,
Your second example probably isn't working because you haven't put user
into the template context. There isn't enough code to know for sure, and you haven't said what bad outcome you are seeing.
Note that if you pass RequestContext
to your templates (which is true most of the time), you already have request.user
aliased simply as {{ user }}
. Then you do just
{{ user.username }}
and the view function needs to know nothing about this.
精彩评论