Django用户显示 - 如何传递用户数据?

时间:2015-07-28 10:28:11

标签: python django django-authentication

我想显示一些我登录的用户的用户名,而django模板将其翻译为空。我的代码:

HTML:

<h3 class="form-title">Welcome '{{ request.user.get_username }}'</h3>

视图:

def login_screen(request,template_name):
    username = password = ''
    args={}
    args.update(csrf(request))
    state = "Please log in below..."
    args['state'] = state
    if request.POST:
        username = request.POST.get('username')
        password = request.POST.get('password')
        if not User.objects.filter(username=username).exists():
            user = User.objects.create_user(username=username,email=username,password=password)
        user = authenticate(username=username, password=password)
        if user is not None:
                login(request, user)
                state = "You're successfully logged in!"
                args['state'] = state
                return render_to_response('good_page.html',args)
        else:
            state = "Your username and/or password were incorrect."
    args['state'] = state
    return render_to_response('login_page.html',args)

THX

1 个答案:

答案 0 :(得分:1)

在您的观看中添加此代码。

args['request'] = request

HTML

<h3 class="form-title">Welcome {{ request.user.username }}</h3>