A decorator is a specific construction in Python (and one that doesn't
appear in your code):

http://www.python.org/dev/peps/pep-0318/#current-implementation-history

http://www.artima.com/weblogs/viewpost.jsp?thread=240808

Good luck!

Franklin

On Wed, Aug 4, 2010 at 9:15 AM, Dan Gentry <dgen...@gmail.com> wrote:
> I'm trying to move some code from a view to a decorator to use with
> many views.  This is the view:
>
> views.py:
> def list_type(request):
>
>    inst_id=request.session.get('inst_id',None)
>
>    ## move to decorator
>    if not inst_id:
>        path = urlquote(request.get_full_path())
>        tup = reverse('select_inst'), REDIRECT_FIELD_NAME, path
>        return HttpResponseRedirect('%s?%s=%s' % tup)
>    ##
>
>    queryset = RecordType.objects.filter(institution=inst_id)
>    return object_list(request, queryset, paginate_by = 12)
>
> I've moved the middle bit of code to a decorator I wrote (basically
> copying user_passes_test), but when I use it I get the strange error
> that my urls.py file contains no patterns.
>
> decorators.py:
> def institution_required(fn, redirect_field_name=REDIRECT_FIELD_NAME):
>
>    inst_url = reverse('select_inst')  ##  URL to select institution
>
>    def _wrapped_view(request, *args, **kwargs):
>        if not request.session.get('inst_id',None):
>            path = urlquote(request.get_full_path())
>            tup = inst_url, redirect_field_name, path
>            return HttpResponseRedirect('%s?%s=%s' % tup)
>        return fn(request, *args, **kwargs)
>    return _wrapped_view
>
>
>
> Would anyone like to school me on the correct way to write a view
> decorator?  Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@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.
>
>



-- 
Art, writing, journal: http://einspruch.com
Comics: http://themoonfellonme.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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