开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜