user full name in template

7,515 views
Skip to first unread message

Bobby Roberts

unread,
Feb 4, 2011, 12:06:47 AM2/4/11
to Django users
is there a template tag to show the user's full name on a template?

arlolra

unread,
Feb 4, 2011, 12:15:00 AM2/4/11
to django...@googlegroups.com

Bobby Roberts

unread,
Feb 4, 2011, 12:24:31 AM2/4/11
to Django users
here's what i'm trying

from django.contrib.auth import authenticate, login, logout
#import django user modules
from django.contrib.auth.models import User

def ShowMainMenu (request):
fullname=User.first_name + ' ' + User.last_name
return render_to_response('scanning/menu.html',
{'fullname':fullname}, context_instance=RequestContext(request))

I'm getting this err:

type object 'User' has no attribute 'first_name'



On Feb 4, 12:15 am, arlolra <arlo...@gmail.com> wrote:
> user.get_full_name should workhttps://github.com/django/django/blob/master/django/contrib/auth/mode...

Andres Lucena

unread,
Feb 4, 2011, 4:15:46 AM2/4/11
to django...@googlegroups.com
On Fri, Feb 4, 2011 at 6:24 AM, Bobby Roberts <tche...@gmail.com> wrote:
> here's what i'm trying
>
> from django.contrib.auth import authenticate, login, logout
> #import django user modules
> from django.contrib.auth.models import User
>
> def ShowMainMenu (request):
>        fullname=User.first_name + ' ' + User.last_name
>        return render_to_response('scanning/menu.html',
> {'fullname':fullname}, context_instance=RequestContext(request))
>
> I'm getting this err:
>
> type object 'User' has no attribute 'first_name'
>
>

I think the problem is that you're trying to get from the class and
not from the instance of the class. So insted of doing that, you
should do something like this:

user = User.objects.get(first_name=bar, last_name=foo)
user.first_name

Of course for doing that you should first get bar and foo (through the
URL, or request.POST, or request.GET)

But I think that what you're looking instead of all that crap is
request.user, so you should do this:

fullname=request.user.first_name + ' ' + request.user.last_name

Or even better:

fullname=request.user.get_full_name

You can skip that step and do this:

def ShowMainMenu (request):
return render_to_response('scanning/menu.html',
{'fullname': request.user.get_full_name,},
context_instance=RequestContext(request))

Or skip the fullname stuff and getting the atribute directly in the
template: {{request.user.get_full_name }}

Don't forget the login_required decorator, because if an anonymous
user try to get that page he will get an exception (I think, didn't
try that)

Ivo Brodien

unread,
Feb 4, 2011, 4:23:53 AM2/4/11
to django...@googlegroups.com

> Don't forget the login_required decorator, because if an anonymous
> user try to get that page he will get an exception (I think, didn't
> try that)

No, they will be redirected to login and after login they come back to the original page, if the form considers the ’next’ field. How nice is that!?

Andres Lucena

unread,
Feb 4, 2011, 4:57:50 AM2/4/11
to django...@googlegroups.com

Yeah, I know, the 'next' magic, with GET or POST, it's veeeery nice ;)

The thing that I was saying is that he should validate that the user
is logged in, (login_required decorator, or
request.user.is_authenticated), because if he doesn't then he will
have an anonymous user instance, and I think that isn't what he
wants...

Thomas

unread,
Feb 4, 2011, 1:42:10 AM2/4/11
to Bobby Roberts, django...@googlegroups.com

which django version do you use?

the current 'head' version shows in django/contrib/auth/models.py:

---8<---

200 class User(models.Model):
201 """
202 Users within the Django authentication system are represented by this model.
203
204 Username and password are required. Other fields are optional.
205 """
206 username = models.CharField(_('username'), max_length=30, unique=True, help_text=_("Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters"))
207 first_name = models.CharField(_('first name'), max_length=30, blank=True)
208 last_name = models.CharField(_('last name'), max_length=30, blank=True)
209 email = models.EmailField(_('e-mail address'), blank=True)

...

---8<---

So there is an attribute called 'first_name'

But, I think the fault is more Python related.

You will need to deal with an existing object of the class 'User'.

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

good luck,
TR

------------------------------------------------

http://thoreg.org


Reply all
Reply to author
Forward
0 new messages