Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从django用户模型获取用户的全名_Python_Django - Fatal编程技术网

Python 从django用户模型获取用户的全名

Python 从django用户模型获取用户的全名,python,django,Python,Django,我试图从django的用户模型中获取数据,并从中打印出用户的全名 view.py from django.shortcuts import render from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User def Showfullname (request): fullname = request.user.get_full_name

我试图从django的用户模型中获取数据,并从中打印出用户的全名

view.py

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User

def Showfullname (request):
    fullname =  request.user.get_full_name()
    context = {'fullname':fullname}
    return render(request, 'main/base.html', context)
url(r'^name$', views.Showfullname, name='Showfullname'),
来自url.py的行

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User

def Showfullname (request):
    fullname =  request.user.get_full_name()
    context = {'fullname':fullname}
    return render(request, 'main/base.html', context)
url(r'^name$', views.Showfullname, name='Showfullname'),
然后,作为我的主文件中的目标的div,我已经在父模板中进行了身份验证检查

<div class="form-group has-feedback">
            <input type="text" class="form-control" placeholder="{% url 'main:Showfullname' %}" readonly />
            <i class="glyphicons glyphicons-car form-control-feedback"></i>
</div>

现在打印出来了


/在应该打印dan dan或admin admin的输入中,您可以在Django模板中使用
{{full_name}}
。此外,如果您的用户经过身份验证,您可以在模板中使用
{{request.user.get_full_name}}

您应该阅读Django教程,因为您显然缺少一些基础知识。对于初学者,将相关部分更改为
placeholder=“{{fullname}}”
。您甚至不需要使用视图来实现此目的,因为只要在任何模板中包含
django.core.context\u processors.auth
context processor,您就可以简单地使用
{user.get\u full\u name}}
,这是正确的答案。@Selcuk将其作为答案编写。这是正确且完美的解决方案。我知道我可以使用{{user.get_full_name}},但我想知道是否有一种方法可以使用基于视图的方法获得相同的结果。不过,我非常感谢大家的回答。