Let's restate the problem.

You want to find view automatically, rather than enumerate them.

Beware, in your existing approaches, that not all functions having a
first argument
named request, and found in files named view.py, are view functions.  Also, not
all view functions meet that description.  Among other things is that,
unlike "models",
there is no magic in the module name "views"; calling your file with
views in it views.py
is just a convention.  There's probably nothing magic about the name
request, either,
since it's pretty clearly a positional parameter.

Nominally, a view is a callable to which a urlpattern points.  We
could argue about
views called by other views, but I digress.  I think that the cleanest
way to find view
functions is by introspecting the urlconf.

But I'm not sure that decorating the view functions is the right
approach.  If, for example,
you have a view function that calls another view function.  Do you
want to log two
traces?  I'd only want one from the outer view function (the one that
was called via a
urlpattern), so that the stack trace would be complete.

If I were doing this, I think I'd figure out how the development
server returns stack
traces (and I'll bet that it's a piece of exception middleware) and
plug in there.

Bill

On Tue, May 10, 2011 at 3:50 PM, Shawn Milochik <sh...@milochik.com> wrote:
> I have a decorator I'd like to apply to every view in a given views.py file.
> Is there an elegant way to do that in Django?
>
> I searched Google and the docs but didn't find anything.
>
> So, I wrote some code which works and demonstrates what I'm trying to do,
> but it's really ugly.
> http://dpaste.com/hold/540845/
>
> Is there a good way to do this?
>
> If you're using __all__ in views.py (which is a great idea anyway), it can
> be MUCH simplified:
> http://dpaste.com/540853/ <-- MUCH MUCH better!
>
> The latter dpaste just might be the hot ticket. But I'm still sending this
> out just in case there's a "best practice" for this.
>
> Thanks,
> Shawn
>
> --
> 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.
>
>

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