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

On Wed, Apr 16, 2008 at 11:01 AM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> 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 wrote:
>  > 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
>  >
>  > On Tue, Apr 15, 2008 at 3:09 PM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> 
> wrote:
>  >
>  >> 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.
>  >>
>  >>
>  >> thank you
>  >>  pavel
>  >>
>  >>  Horst Gutmann wrote:
>  >>  > 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
>  >>  >
>  >>  > On Tue, Apr 15, 2008 at 2:19 PM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> 
> wrote:
>  >>  >
>  >>  >> 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
>  >>  >>
>  >>  >>
>  >>  >>
>  >>  >>  Horst Gutmann wrote:
>  >>  >>  > 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
>  >>  >>  >
>  >>  >>  > On Tue, Apr 15, 2008 at 2:05 PM, [EMAIL PROTECTED] <[EMAIL 
> PROTECTED]> wrote:
>  >>  >>  >
>  >>  >>  >> 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
>  >>  >>  >>
>  >>  >>  >>  >
>  >>  >>  >>
>  >>  >>  >>
>  >>  >>  >
>  >>  >>  > >
>  >>  >>  >
>  >>  >>
>  >>  >>
>  >>  >>  >
>  >>  >>
>  >>  >>
>  >>  >
>  >>  > >
>  >>  >
>  >>
>  >>
>  >>  >
>  >>
>  >>
>  >
>  > >
>  >
>
>
>  >
>

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to