在 Python (Django) 中将一个函数调用到另一个函数中

Cos*_*tin 2 python django

我正在尝试将函数调用到 django 中的函数中,但我不断收到:

The view app.views.function1 didn't return an HttpResponse object. It returned None instead.
Run Code Online (Sandbox Code Playgroud)

我的观点是:

def function1(request):
    [some api calls] 
    #Once this process is done I want to call my second function
    function2()
Run Code Online (Sandbox Code Playgroud)

然后我有

def function2(request):
Run Code Online (Sandbox Code Playgroud)

如何轻松调用 Django/Python 中的其他函数

ps 这些函数可能只是一个,我只是想将它们分开以使我的代码更具可读性并且让一个函数只做一件事。

Avi*_*mka 6

错误很明显:

视图 app.views.function1 没有返回 HttpResponse 对象。它返回 None 。

你需要HttpResponse在完成时返回一个对象function1,所以如果function2是你的最后一个函数,它应该返回该HttpResponse对象,你也应该返回该函数的结果:

def function1(request):
    [some api calls] 
    #Once this process is done I want to call my second function
    return function2(request)

def function2(request):
    # some hard work
    return HttpResponse(...)
Run Code Online (Sandbox Code Playgroud)