Django 1.4 - {{request.user.username}}不在模板中呈现 [英] Django 1.4 - {{ request.user.username}} doesn't render in template

查看:239
本文介绍了Django 1.4 - {{request.user.username}}不在模板中呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,我可以打印request.user.username,但是在模板中,{{request.user.username}}不会出现。为了方便起见,我从函数中删除了逻辑,我正在导入render_to_response& RequestContext。

In my view, I can print request.user.username, however in the template, {{request.user.username}} does not appear. To make this easy, I removed the logic from the function, and I am importing render_to_response & RequestContext.

from django.shortcuts import render_to_response
from django.template import RequestContext

@login_required
@csrf_protect
def form(request):
    print request.user.username
    data = {}
    return render_to_response('form.html', data, context_instance=RequestContext(request))   

我的猜测是我的settings.py有问题。

My guess is that I have problem with my settings.py.

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'src',
)

提前感谢您的帮助 -

Thanks in advance for your help-

推荐答案

As 在文档中提到,经过身份验证的用户的对象存储在模板中的用户变量中。所提到的文档包括以下示例:

As mentioned in the documentation, authenticated user's object is stored within user variable in templates. Mentioned documentation includes the following example:


当渲染模板 RequestContext 时,目前登录用户,用户实例或 AnonymousUser 实例,存储在模板变量 {{user}}

When rendering a template RequestContext, the currently logged-in user, either a User instance or an AnonymousUser instance, is stored in the template variable {{ user }}:



{% if user.is_authenticated %}
    <p>Welcome, {{ user.get_username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please log in.</p>
{% endif %}

编辑:感谢@缓冲区,谁挖出这个旧答案,我已经更新了最近的状态。当它原本写在Django 1.4(在2012年3月底发布)不到一个月的时间里,这是正确的。但是,由于Django 1.5,要获取用户名的正确方法是调用 get_username() 在用户模型实例上。由于能够交换用户类(并将自定义字段用作用户名),因此已添加。

Thanks to @buffer, who dug out this old answer, I have updated it with most recent state. When it was originally written, in less than a month after Django 1.4 (which was released at the end of March 2012), it was correct. But since Django 1.5, proper method to get username is to call get_username() on user model instance. This has been added due to ability to swap User class (and have custom field as username).

这篇关于Django 1.4 - {{request.user.username}}不在模板中呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆