Login

Decorator for enabling views only when developing

Author:
damd
Posted:
August 6, 2010
Language:
Python
Version:
1.2
Tags:
view settings url debug decorator 404
Score:
2 (after 4 ratings)

This is a decorator which essentially replaces the decorated view with a view that always raises Http404 (File Not Found) when settings.DEBUG is set to True.

 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. JSON decorator for views handling ajax requests by anilshanbhag 2 years, 2 months ago
  2. Header view decorators by ydm 2 years ago
  3. Auto-resolving a specific object from key string in url with decorator by achimnol 5 years, 7 months ago
  4. Internal view decorator by gsakkis 4 years, 7 months ago
  5. Remember path decorator by robhudson 5 years, 2 months ago

Comments

bkeating (on August 6, 2010):

Excellent! Perfect timing --already using it.

#

Please login first before commenting.