Wednesday, August 4, 2010

Re: View Decorator

On Aug 4, 2:15 pm, 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.

Can you show how you're using that decorator in your views - and also
your urls.py, since there seems to be a problem there?
--
DR.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment