Re: Does Django have a function that can be called by every view function ?

2008-12-01 Thread David Shieh

Thank you , guys ~
Your help are really good for me ~

On Dec 1, 11:23 pm, Jon Loyens <[EMAIL PROTECTED]> wrote:
> Another +1 for decorators.  In fact, the example David S uses
> authentication and logins as an example of what he wants to do and its
> already handled as a decorator thus giving him a decent pattern to
> follow.  David S, I'd suggest you look up the @login_required
> decorator and even look through the Django code to see how it's
> implemented.  Don't be afraid of cruising through the code.  As good
> as the documentation is, the code is also well organized and uses a
> lot of good pythonic idioms if you're new to the language.
>
> Jon.
>
> On Dec 1, 6:49 am, martyn <[EMAIL PROTECTED]> wrote:
>
> > David,
>
> > +1 for decorator.
> > That let you use it or not in each view.
>
> > On Dec 1, 9:30 am, David Shieh <[EMAIL PROTECTED]> wrote:
>
> > > Thanks , David Zhou , I will find some information for decorator
> > > And also , I will surf for middleware .
>
> > > Thank you very much , Malcolm.
>
> > > On Dec 1, 3:55 pm, "David Zhou" <[EMAIL PROTECTED]> wrote:
>
> > > > On Mon, Dec 1, 2008 at 2:23 AM, David Shieh <[EMAIL PROTECTED]> wrote:
> > > > > I dont' know whether this make any sense .
> > > > > Right now , I can't test it , but if django won't initiate the
> > > > > views.py as a class , this method will make no sense.
>
> > > > Why not write a decorator?
>
> > > > ---
> > > > David Zhou
> > > > [EMAIL PROTECTED]
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Does Django have a function that can be called by every view function ?

2008-12-01 Thread David Shieh

Thanks , David Zhou , I will find some information for decorator
And also , I will surf for middleware .

Thank you very much , Malcolm.

On Dec 1, 3:55 pm, "David Zhou" <[EMAIL PROTECTED]> wrote:
> On Mon, Dec 1, 2008 at 2:23 AM, David Shieh <[EMAIL PROTECTED]> wrote:
> > I dont' know whether this make any sense .
> > Right now , I can't test it , but if django won't initiate the
> > views.py as a class , this method will make no sense.
>
> Why not write a decorator?
>
> ---
> David Zhou
> [EMAIL PROTECTED]
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Does Django have a function that can be called by every view function ?

2008-11-30 Thread David Shieh

I have found a post in here :
http://davyd.livejournal.com/262859.html

this is the way this author resolves this problem.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Does Django have a function that can be called by every view function ?

2008-11-30 Thread David Shieh

No , not in that way.
Let me show you some code

#a.py
class A:
 def __init__(self):
  #do somthing
#views.py
class B(A):
 def index(request):
  #do something else
#urls.py
(r'^$', 'site.app.views.index'),


I dont' know whether this make any sense .
Right now , I can't test it , but if django won't initiate the
views.py as a class , this method will make no sense.


On Dec 1, 2:52 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Sun, 2008-11-30 at 22:46 -0800, David Shieh wrote:
> > yeah , I will consider about middleware , and I am also thinking about
> > write a class that contains the functions I need , and other funcions
> > just inherit it , will it work ?
>
> Since functions don't inherit from classes, your question doesn't really
> make sense.
>
> Regards,
> Malcolm
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Does Django have a function that can be called by every view function ?

2008-11-30 Thread David Shieh

yeah , I will consider about middleware , and I am also thinking about
write a class that contains the functions I need , and other funcions
just inherit it , will it work ?

On Dec 1, 2:41 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Sun, 2008-11-30 at 22:35 -0800, David Shieh wrote:
> > Hi , Malcolm,
>
> > Now , I understand what you said.But using session means user must to
> > call a certain to set the session.
>
> I was using session as an *example*. It's something that, based on
> incoming request information, is run every time and can do some extra
> processing. It's an example of how to abstract away that processing from
> the view functions.
>
> > Maybe Django should learn from other frameworks to get a action system
> > to handle this.
>
> You keep operating on the assumption that it doesn't exist, but that
> seems to be because you're locked into it being a way to pass extra
> parameters to a view. Django already has such a system. It's called
> middleware.
>
> Malcolm
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Does Django have a function that can be called by every view function ?

2008-11-30 Thread David Shieh

Hi , Malcolm,

Now , I understand what you said.But using session means user must to
call a certain to set the session.I mean , for example , I am setting
up a blog , and every page want to show the categories this blog
have . If I write these categories as a session value , I have to
write it into the index function ,and when a user visit the index
page , he will get it ,and no matter which page he visit , he will
also get this value . However  , the problem is , what if he firstly
visit one of the other article page , maybe he just get the url for
some page , so , he won't get the categories value , cause he didn't
visit the index page , am I right ?

Maybe Django should learn from other frameworks to get a action system
to handle this.

Indeed , I really need your help with this . Thanks

On Dec 1, 2:22 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Sun, 2008-11-30 at 21:48 -0800, David Shieh wrote:
> > Thanks , Malcolm ,
>
> > I think your solution can resolve my problem.But does it a bit
> > complex ?
> > In fact , what I really want is a view function that will be called by
> > every view function.
> > i.e. , if I wrote an auth system , I need to authenticate whether the
> > user is logged in every view functions and pass some parameters to
> > every views function .
>
> > Any suggestions ?
>
> You'll end up dispatching every URL to the same view and then
> essentially building your own dispatcher to further dispatch to the
> "real" view functions. Or attach a decorator to those views you want
> this to happen to.
>
> Because, the point is, you rarely want such a thing to run for *every*
> view function. In any significantly sized application, there is rarely
> common stuff that runs for every single view that cannot be done via
> middleware. You're already receiving a request object in your functions,
> you can pass in extra parameters through that (just like we do with
> request.session, etc).
>
> In other words, the functionality already exists. It's spelled slightly
> differently to other frameworks, but that's pretty normal -- everybody
> has slightly different ways of writing the same thing.
>
> The option you are looking for is middleware. If you don't want to use
> that, that's fine and writing a wrapper to make it all automatic is
> obviously possible. But it's not shipped with Django, since we already
> provide one way to achieve this functionality.
>
> Regards,
> Malcolm
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Does Django have a function that can be called by every view function ?

2008-11-30 Thread David Shieh

Thanks , Malcolm ,

I think your solution can resolve my problem.But does it a bit
complex ?
In fact , what I really want is a view function that will be called by
every view function.
i.e. , if I wrote an auth system , I need to authenticate whether the
user is logged in every view functions and pass some parameters to
every views function .

Any suggestions ?

On Dec 1, 11:49 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Sun, 2008-11-30 at 19:41 -0800, David Shieh wrote:
>
> [...]
>
> > I have searched for some information . I know that middleware can do
> > this , but what I really want is just a small function for this .
>
> A process_request() function in middleware can be as small as you like.
> Middleware provides the hooks for functions that run before and/or after
> each view function. It's exactly what you're asking about and you
> haven't explained why they don't do what you want.
>
> Regards,
> Malcolm
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Does Django have a function that can be called by every view function ?

2008-11-30 Thread David Shieh

I have used Django for several months , and it really attacts me . I
love it.
But when I use it more , I found a problem troubles me , that is , I
can't find a way to make a function be called by every view function
automatically .

I used CakePHP , it have a function named beforeFilter() . It will be
called everytime before the action excute . That is , when a user
calles an action , this beforeFilter() fucntion is called by CakePHP
automatically , very convenient. So I think , does Django have the
same function like CakePHP ? And as I know , other frameworks also
have the same function , like RoR .

I have searched for some information . I know that middleware can do
this , but what I really want is just a small function for this .

So , guys , please help me with this , thanks.

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---