On 17 déc, 17:08, abidibo <abid...@gmail.com> wrote:
> Hi all,
> I'm a beginenr with Django and Python

And possibly with web programming AFAICT - but we won't hold it
against you !-)

> and I'e have a problem I didn't
> reached to solved myself, here is the question:
> I'd like to have a DB table like POST, that contains a title, a text
> and an author. I'd like the field author to be filled with the id of
> the user which is writing the form, so i tried to set it as a default
> value in the models.py

You can't. Period. The "user which is writing the form" is only known
when there's an incoming HTTP request (from a logged in user, of
course), which is not the case by the time the models are loaded.

IOW, you have to wait until there's an available logged-in user (that
is : in the view handling your form), and explicitely pass it to the
model (eventually thru the form).

> this way:
>
>   from django.db import models
>   from django.contrib.sessions.models import Session
>   from django.http import HttpRequest
>
>   request = HttpRequest()

The HttpRequest class is used by Django to present the incoming HTTP
request (a chunk of bits sent thru the wires) as a Python object.
You're not supposed to instanciate it yourself (except eventually for
unittesting), but to get it as first param of a view function.

Since you obviously don't get it, here's how things work (8-miles
heigh view, skipping quite a lot of steps):

1/ you start your Django server process (either the builtin dev
server, as a wsgi or fcgi server or when starting Apache if you're
using mod_python)

2/ this process loads all your models

3/ then he waits for incoming HTTP requests

4/ when a request come in, django
41. build an HttpRequest object from it
4.2 uses the urls.py to find out which view function should handle it
4.3 call the function with the HttpRequest instance as first param

5. the view function do whatever it has to do - usually using some
model instance and
5.1 returns an HttpResponse object

6. django builds a 'real' HTTP response (ie : a chunk of bits that
will be sent thru the wires) from the (python) HttpResponse object

(snip)

> but the problem, i think, is that the array cookies has no elements,

indeed.

> even if with my browser tool i may see the cookie with key 'sessionid'
> setted.

indeed. There's absolutely _no_ (none, zilch, nada) relationship
between the HTTP request sent by your browser and the HttpRequest
object you instanciate in your models.py file.

If your form is handled thru a view function (and assuming you have
the appropriate middleware installed, cf documentation), you'll get
access to the currently logged-in user as request.user. Then proceed
as you would with any other model attribute. If you want to handle
this for admin forms, just follow this link:
http://code.djangoproject.com/wiki/CookBookNewformsAdminAndUser

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

Reply via email to