how to get a reference to a authenticated user

10 views
Skip to first unread message

Benedict Verheyen

unread,
Feb 20, 2007, 3:58:51 AM2/20/07
to django...@googlegroups.com
Hi,

i have some tables that i want to link with an authorized user.
Right now i use the login mechanism of django.

1. Is it possible to specify a foreign_key to the auth_user database?
If not, what is the best way to have a "user" field in a table
that refers to a logged in user?

2. How can i get the user id and username available in my templates.
I would like to display the current logged in user and use the id
to automatically fill in, in the field specified in 1.

Thanks,
Benedict

Honza Král

unread,
Feb 20, 2007, 4:27:50 AM2/20/07
to django...@googlegroups.com
On 2/20/07, Benedict Verheyen <benedict...@gmail.com> wrote:
>
> Hi,
>
> i have some tables that i want to link with an authorized user.
> Right now i use the login mechanism of django.
>
> 1. Is it possible to specify a foreign_key to the auth_user database?
> If not, what is the best way to have a "user" field in a table
> that refers to a logged in user?

Sure, its just a django application with models and stuff...

>
> 2. How can i get the user id and username available in my templates.
> I would like to display the current logged in user and use the id
> to automatically fill in, in the field specified in 1.

to have it in your templates simply use the auth middleware and add
django.core.context_processors.auth to your TEMPLATE_CONTEXT_PROCESSORS

>
> Thanks,
> Benedict
>
> >
>


--
Honza Král
E-Mail: Honza...@gmail.com
ICQ#: 107471613
Phone: +420 606 678585

Russell Keith-Magee

unread,
Feb 20, 2007, 4:28:37 AM2/20/07
to django...@googlegroups.com
On 2/20/07, Benedict Verheyen <benedict...@gmail.com> wrote:
>
> Hi,
>
> i have some tables that i want to link with an authorized user.
> Right now i use the login mechanism of django.
>
> 1. Is it possible to specify a foreign_key to the auth_user database?

Yes.

from django.db import models
from django.contrib.auth.models import User

class MyModel(models.Model):
user = models.ForeignKey(User)
# ... rest of your model here.

> 2. How can i get the user id and username available in my templates.

Instances of User are the same as any other model instance; if you
have a user instance 'myuser' you can ask for myuser.username or
myuser.id. If you pass myuser in as part of the template context, you
can reference {{ myuser.username }} or {{ myuser.id }} in the
template.

> I would like to display the current logged in user and use the id
> to automatically fill in, in the field specified in 1.

In the view:

def my_view(request):
... view logic here

The currently logged in user is available as request.user. You can use
this value to modify or prefill any form you construct. If you
construct a RequestContext to provide to the template, this will
expose the currently logged in user under the tag 'user' in the
template.

Yours,
Russ Magee %-)

Honza Král

unread,
Feb 20, 2007, 4:29:30 AM2/20/07
to django...@googlegroups.com
On 2/20/07, Honza Král <honza...@gmail.com> wrote:
> On 2/20/07, Benedict Verheyen <benedict...@gmail.com> wrote:
> >
> > Hi,
> >
> > i have some tables that i want to link with an authorized user.
> > Right now i use the login mechanism of django.
> >
> > 1. Is it possible to specify a foreign_key to the auth_user database?
> > If not, what is the best way to have a "user" field in a table
> > that refers to a logged in user?
>
> Sure, its just a django application with models and stuff...
>
> >
> > 2. How can i get the user id and username available in my templates.
> > I would like to display the current logged in user and use the id
> > to automatically fill in, in the field specified in 1.
>
> to have it in your templates simply use the auth middleware and add
> django.core.context_processors.auth to your TEMPLATE_CONTEXT_PROCESSORS

to have the user available outside templates and views (models etc.) see

http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser

Benedict Verheyen

unread,
Feb 20, 2007, 5:11:45 AM2/20/07
to django...@googlegroups.com
Russell Keith-Magee schreef:
<snip>

Thanks for the answers (super quick !) Russell en Honza.
I'll try it out at once,

Thanks,
Benedict

Reply all
Reply to author
Forward
0 new messages