Decorator for enabling views only when developing

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def when_developing(view):
    """
    Usage:

    @when_developing
    def delete_all_users(request):
        User.objects.all().delete()
        return HttpResponse('Successfully deleted all users.')
    """

    from django.conf import settings

    def f404(*args, **kwargs):
        from django.http import Http404
        raise Http404

    def inner(*args, **kwargs):
        return view(*args, **kwargs)

    if not settings.DEBUG:
        return f404
    return inner

More like this

  1. get_model_or_404 by blackbrrr 4 years, 8 months ago
  2. Middleware to prevent access to the admin when user ip not in INTERNAL_IPS by jezdez 2 years, 10 months ago
  3. add port from settings file to an url by sebnapi 11 months ago
  4. View decorator to convert DoesNotExist (ObjectDoesNotExist) exceptions into Http404 exceptions by jammycakes 3 years, 7 months ago
  5. Smart append slash middleware by akaihola 5 years, 3 months ago

Comments

bkeating (on August 6, 2010):

Excellent! Perfect timing --already using it.

#

(Forgotten your password?)