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
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
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 %-)
to have the user available outside templates and views (models etc.) see
http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
Thanks for the answers (super quick !) Russell en Honza.
I'll try it out at once,
Thanks,
Benedict