Skip to content

Instantly share code, notes, and snippets.

@shangxiao
Created May 14, 2016 13:20
Show Gist options
  • Save shangxiao/db2924247238862f83f34a083730991a to your computer and use it in GitHub Desktop.
Save shangxiao/db2924247238862f83f34a083730991a to your computer and use it in GitHub Desktop.
Django view decorator for returning 404 if not authenticated
from functools import update_wrapper
from django.http import Http404
class authenticated_or_404(object):
def __init__(self, func):
self.func = func
update_wrapper(self, func)
def __call__(self, request, *args, **kwargs):
if not request.user.is_authenticated():
raise Http404
return self.func(request, *args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment