Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get fullname of user from django user model

Tags:

python

django

I am trying to get the data from django's user model and print the full name of a user from it.

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)

the line for it from urls.py

url(r'^name$', views.Showfullname, name='Showfullname'),

and then the div that is the target in my main files, where I already have a is authenticated check in a parent template.

<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>

Right now it prints

/ in the input when it should be printing dan dan or admin admin

like image 974
Hangfish Avatar asked Oct 17 '22 20:10

Hangfish


2 Answers

You can use {{full_name}} in your Django template. Also, if your user is authenticated you can use {{request.user.get_full_name}} in the template.

like image 146
German Jr Avatar answered Oct 20 '22 09:10

German Jr


In the template, you can use this to get the full name

<h3>{{request.user.get_full_name}}</h3>

it returns the first_name plus the last_name, with a space in between. docs

like image 32
Ankit Kumar Avatar answered Oct 20 '22 09:10

Ankit Kumar