你的错误是,在myView
函数中,你调用了你的checkFunction
,但是你没有使用的返回值,checkFunction
所以你的返回值 checkFunction
return HttpResponse(status=403)
丢失了并且永远不会在 中返回myView
。
它可能是这样的:
def myView(request):
result = checkFunction()
if result:
return result
#if no problem, keep running on...
def checkFunction():
#do stuff
if something_goes_wrong:
return HttpResponse(status=403)
# you do not need to return anything if no error occured...
因此,如果没有出错,则checkFunction
不会返回任何内容,并且将result
不会执行 block。如果您返回响应,那么您的视图将返回该响应(在您的情况下,)...None
if result:
HttpResponse(status=403)
更新:那么你可以这样做....
def checkFunction(request):
#do stuff
if something_goes_wrong:
return HttpResponse(status=403)
elif some_other_issue:
return HttpResponse(....)
else: #no problems, everything is as expected...
return render_to_response(...) # or any kind of response you want
def myView(request):
return checkFunction(request)
这样,您的视图将返回您checkFunction
返回的内容...
此外,可能需要将request
对象传递给您,因为您希望在那里创建响应。checkFunction
你可能需要它。