I have written two very simple decorators for my django app :
def login_required_json(f):
def inner(request, *args, **kwargs):
#this check the session if userid key exist, if not it will redirect to login page
if not request.user.is_authenticated():
result=dict()
result["success"]=False
result["message"]="The user is not authenticated"
return HttpResponse(content=simplejson.dumps(result),mimetype="application/json")
else:
return f(request, *args, **kwargs)
def catch_404_json(f):
def inner(*args,**kwargs):
try:
return f(*args, **kwargs)
except Http404:
result=dict()
result["success"]=False
result["message"]="The some of the resources throw 404"
return HttpResponse(content=simplejson.dumps(result),mimetype="application/json")
But when I apply them to my views I get “ViewDoesNotExist” error in the template, saying it could not import the view because it is not callable. What am i doing wrong?
Your decorator is returning None, instead of the actual view.
So return the inner function as I’ve demonstrated above.