1
votes

I have created a decorator to check authentication before my view. I'm planning on putting this decorator on all views. I've determined that my code is getting executed. However I'm getting the above error.

Here is my decorator: '''

def check_the_permissions(func):
   def wrap(request):
      soeid = request.GET.get('soeid')
      if not soeid:
          return HttpResponse("Please ..")
      is_allowed = getEEMSResponse(soeid, URL)
      if not is_allowed:
          return HttpResponse("You do not have ...")
      func(request)
   return wrap

@check_the_permissions
def LimitVsUsageTrend(request):
   scatterLimitVsUsage = plot(myfigure, output_type='div')
   return render(request, 'LimitVsUsageTrend.html", {'myplot': scatterLimitVsUsage})

'''

Here is my stack trace Traceback (most recent call last): File "C:\Users\opt\exeception.py" line 34 in inner response = get_response(request) File "C:\Users\opt\base.py", line 126 in _get_response "returned None instead." % (callback.module, view_name) ValueError: The view plots.views.wrap didn't return an HttpResponse object. It returned None instead.

1

1 Answers

1
votes

You forgot to return the result of the func(request) call. I furthermore would also advice to pass *args and **kwargs to make the decorator applicable to functions that accept extra parameters as well:

def check_the_permissions(func):
   def wrap(request, *args, **kwargs):
      soeid = request.GET.get('soeid')
      if not soeid:
          return HttpResponse('Please ..')
      is_allowed = getEEMSResponse(soeid, URL)
      if not is_allowed:
          return HttpResponse('You do not have ...')
      return func(request, *args, **kwargs)
   return wrap