session data or ???

14 views
Skip to first unread message

hkm...@gmail.com

unread,
Apr 15, 2008, 8:05:44 AM4/15/08
to django...@googlegroups.com
hi all

please , i would like to ask for a little help. I have few functions
stored in my views.py under class Test. When i visit my homepage the
urls.py is calling method homepage from views.py as i mentioned before.
Here it dig some data as current server time and user agent from
webbrowser. Now i need to pass these two details to any other function
when the function is called. I read all docs about session, cache and
others, but still did not found an answer.
I also tried to store these details to session with

s = SessionStore(session_key='2b2289a188b44ad18c35e113ac6ceead')
s['last_login'] = datetime.datetime.now()
s.save()

and later call it. It saved it but with different session_key.
So back to the first problem. Please how can i pass some data from one
method to all methods.

thank you very much for helping me
pavel

Horst Gutmann

unread,
Apr 15, 2008, 8:14:00 AM4/15/08
to django...@googlegroups.com
Sorry if I misunderstood your problem, why don't you simply store the
last_login somewhere in a userprofile? Since -- judging from the name
-- it should only be altered when the user really logs in, I don't see
the relation with a session there :-/

-- Horst

hkm...@gmail.com

unread,
Apr 15, 2008, 8:19:41 AM4/15/08
to django...@googlegroups.com
hi Horst

the last_login is an example from documentation. My problems are, that
when i save something to the session(as current date, or user agent)
with some key, and then look to database from terminal, the key is
different.

The second problem is, that i cannot find a way how to pass some data
from one function to all other functions.

thank you
pavel

wkn

unread,
Apr 15, 2008, 8:43:46 AM4/15/08
to Django users

I think the current documentation and implementation of the
SessionStore is not correct. I had some problems with it yesterday as
well. I eventually solved it differently and without a session.

However if you just want to access a session from across different
views you can just use the 'request.session' dictionary. No need for
using the SessionStore.

But like Horst said try to figure out if you really need to use a
session in the first place.

Wim

Horst Gutmann

unread,
Apr 15, 2008, 8:45:27 AM4/15/08
to django...@googlegroups.com
Why do you want to manipulate just a specific session that might not
even be an existing sesion anymore? And for what reason do you want to
give other views the same data as the last view that's been used? To
me this sounds just like using a normal session (as provided by
request.session) ...

Could you perhaps tell a few more details about what you want to
achieve and for what reason? :-)

-- Horst

hkm...@gmail.com

unread,
Apr 15, 2008, 9:09:37 AM4/15/08
to django...@googlegroups.com
ok i will try to explain.

when i or whoever visit homepage, the method from views.py generates
some data, for example
info about operating system or whatever, just some value stored in
variable or dict, let's call it
info = {'system': 'unix'}

later when user is browsing the site, he/she visits some other part of
site when some other method is called. And i need to access this variable.
So does it mean that in every method where i need to work with this
variable i have to save it to sessions in first method and use
request.session in method where i need to work with this variable, or is
there some better way how to pass this variable?
Please feel free to show me some code examples.

Horst Gutmann

unread,
Apr 15, 2008, 9:17:57 AM4/15/08
to django...@googlegroups.com
Well, depending on how you handle this "variable" you have multiple options:

1. Store it into the session using request.session['info'] = info and
retrieve it later from the session when you want to use this data.
2. If you also want to keep this "choice" permanently associated with
the user, I'd store it in the user's profile

IMO these are the only options you have and they are not really bad
ones. Naturally you could also simply pass that choice using GET
parameters, but again: This depends on how you want to use this value.

-- Horst

hkm...@gmail.com

unread,
Apr 16, 2008, 5:01:46 AM4/16/08
to django...@googlegroups.com
great, thank you very much Horst

now it is working, but still have some question please.

for instance in my views.py i have:

index_globals={}
Class Test:
init and some more data ....

def homepage(request):
some code with variables ....
index_globals['info'] = user_system
index_globals['timestamp'] = today
request.session['index_globals'] = index_globals

so now when i need some data from index_globals in every method i have
to call with

user_start_time = request.session['index_globals']['timestamp']

but what about if i need in my every method the current url path
current_path = request.path

or this index_globals

is there a way i can make this variable global(the current_path will
change every time when some method in views.py will be called), and it
will be automaticaly assign to locals() in every method.
just like

current_path = request.path
Class Test:

i hope i describe it well, so you will undestand
thank you very much again
pavel

Horst Gutmann

unread,
Apr 17, 2008, 6:01:57 PM4/17/08
to django...@googlegroups.com
Well, you could minimize the amount of code you will have to repeat
for each and every view function by doing some OOP. For instance you
could probably do something like this in your views.py:

class AbstractView(object):
def __init__(self, request, *args, **kwargs):
# Do some generic stuff here
pass
def __call__(self, *args, **kwargs):
raise RuntimeError, "Not implemented"

class TestView(AbstractView):
def __call__(self, *args, **kwargs):
return render_to_response('some_template.html',{})

def create_view(klass):
def _func(request, *args, **kwargs):
return klass(request, *args, **kwargs)()
return _func

test = create_view(TestView)

This module provides the view "test".
In this case, you'd put the whole generic stuff you want _all_ your
views to perform into the __init__ method of the AbstractView class.
This is just a quick solution, but it should give you an idea of how
to share processing between views if the short processing requires
request-specific data.

I hope this helps (and isn't a really stupid solution for your problem ;-) )

-- Horst

hkm...@gmail.com

unread,
Apr 22, 2008, 8:25:27 AM4/22/08
to django...@googlegroups.com
hi all

please i have another question about some data. I have created a model
for user profile in my app. Edited the settings, and it is working ok.

So when user logged in through this method i save this to variable and i
am able to call it from this method. But what if i needed to call this
from all methods in my class. Do i have to save this to session with a
secret key(as for every visitor it should be unique), and pass the key
in cookies all the time, and from every method parse the cookie, catch
the secret key, and dig data with it from session.

i believe there is some nicer way, isn't it?

thank you very much
pavel

hkm...@gmail.com

unread,
Apr 22, 2008, 9:31:13 AM4/22/08
to django...@googlegroups.com
hi all

please i am unable to find how to obtain sesssion key after i create the
session.
from
http://www.djangoproject.com/documentation/sessions/#using-sessions-out-of-views

i save some data

from django.contrib.sessions.backends.db import SessionStore

s = SessionStore(session_key='')
s['name'] = 'pavel'
s.save()

so it creates new row in django_session table, but how will i obtain now
the session key to retrive the session data?

thank you very much for advice
pavel


hkm...@gmail.com

unread,
Aug 15, 2008, 5:26:59 AM8/15/08
to django...@googlegroups.com
hi all

please i would like know if there is a way how to get latest 0.95.3 to
my debian etch system. as i have official stable src(i am only able to
use stable) and only 0.95.1-1 version is available for me.

thank you very much, pavel


Nick Rehm

unread,
Aug 15, 2008, 6:48:27 AM8/15/08
to django...@googlegroups.com
You still can use the latest svn checkout on debian etch.
Like i do. Update cycles on debian can take a long time.

http://www.djangoproject.com/download/

And if you only want to use 0.95.3
http://www.djangoproject.com/download/0.95.3/tarball/
get this tarball

hkm...@gmail.com schrieb:

Brett Parker

unread,
Aug 15, 2008, 6:49:03 AM8/15/08
to django...@googlegroups.com

There'll be a new package for the 1.0 beta in the next few days, that'll
most likely go to the experimental distribution - unstable currently has
0.96.2, which could easily be backported to etch. Lenny, currently, also
has the 0.96.2 package, and as lenny is now in freeze is likely to
release with that.

Also, the version that is in debian etch has the patches from 0.95.2 and
0.95.3 merged in to the 0.95.1 package for etch (it being just security
updates, debian policy dictates that we patch and keep the version
number the same).

I'll be creating the 1.0 beta packages this evening, but can (if you
want) create some etch 0.96.2 packages.

Thanks,
--
Brett Parker

hkm...@gmail.com

unread,
Aug 15, 2008, 7:00:28 AM8/15/08
to django...@googlegroups.com
thanks for replay Nick. i know about tar source, but what i need is to
have latest stable version installed via apt-get(without creating own
deb file from tar) as the system on all machines is done this way to
keep everything in order. I know the django developers are mostly
updating svn version, but i expected that maintainer for this package
will create update version in which the XSS bug is fixed.

anyway thanks again for answer and have a nice weekend
pavel

Brett Parker

unread,
Aug 15, 2008, 7:27:26 AM8/15/08
to django...@googlegroups.com
On 15 Aug 13:00, hkm...@gmail.com wrote:
>
> thanks for replay Nick. i know about tar source, but what i need is to
> have latest stable version installed via apt-get(without creating own
> deb file from tar) as the system on all machines is done this way to
> keep everything in order. I know the django developers are mostly
> updating svn version, but i expected that maintainer for this package
> will create update version in which the XSS bug is fixed.

In the debian stable archive you should find python-django-0.95.1-1etch1
(see: http://packages.debian.org/etch/python-django), if you look at the
changelog for that
(http://packages.debian.org/changelogs/pool/main/p/python-django/python-django_0.95.1-1etch1/changelog)
you'll see that the xss bug is patched in that version.

Thanks,
--
Brett Parker

pavel srb

unread,
Aug 15, 2008, 4:39:08 PM8/15/08
to django...@googlegroups.com
great, thank you very much. now it is all clear for me. I had my debian package from past time. So now i know i should always check for Debian change log. Reinstalling the package will solve my problem.

thanks again, enjoy weekend
pavel
Reply all
Reply to author
Forward
0 new messages