[Answer]-Going to another view

1👍

If its only purpose is what you’ve described, you should consider writing protect as a view decorator. This answer provides one example of how to do so.

Based on view decorators that I have written, your protect decorator could look something like:

from functools import wraps

from django.utils.decorators import available_attrs

def protect(func):
    @wraps(func, assigned=available_attrs(func))
    def inner(request, *args, **kwargs):
        if some_condition:
            return render_to_response('protected_template')
        return func(request, *args, **kwargs)
    return inner

Which would allow you to then use it like:

@protect
def show(request):
   ...
   return render_to_response(...)
👤dgel

Leave a comment