Django用户类(django.contrib.auth)fname无法正常工作

时间:2015-10-31 22:45:18

标签: django django-authentication django-users

在我的网站中,我无法显示user.fname。用户中的其他所有内容,如user.username,user.email和user.password都有效,但我无法显示名称。

PS:我已尝试过user.fname,user.name,user.name,user.first_name到目前为止,没有显示任何内容

编辑:

user = request.user
if request.user.is_authenticated():
    fname = request.user.get_short_name()
return render(request, 'student/dashboard.html', {'user': user, 'fname': fname})

我添加了这个,但由于某种原因我仍然无法调用它(这是我的观点)

class RegisterForm(forms.Form):
        GRADE_CHOICES = ( 
                    (9,'9'), (10,'10'), (11,'11'), (12,'12') , 
                )
        curr_year = date.today().year
        GRAD_YEAR_CHOICES = ( 
                    (curr_year,curr_year), (curr_year+1,curr_year+1), (curr_year+2,curr_year+2), (curr_year+3,curr_year+3) , 
                     )
        fname = forms.CharField(max_length = 25)
        lname = forms.CharField( max_length = 25)
        emailid = forms.EmailField()
        passwd1 = forms.CharField(max_length=100,widget=forms.PasswordInput)
        passwd2 = forms.CharField(max_length=100,widget=forms.PasswordInput)
        gradyear = forms.ChoiceField( choices=GRAD_YEAR_CHOICES)
        grade = forms.ChoiceField( choices=GRADE_CHOICES)

这是在我的forms.py

1 个答案:

答案 0 :(得分:0)

尝试

 user.first_name
 user.last_name

如果用户已登录,则可以在您的视图中使用:

if request.user.is_authenticated():
    username = request.user.get_username()

更多信息:https://docs.djangoproject.com/en/1.8/ref/contrib/auth/