django - 在 Django 的 View 函数中调用函数

标签 django django-views

1 或 2 是在 Django View 函数中调用函数的正确方法吗?或者两者都同样可以。请解释一下。

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

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

def function2():
    # some hard work
    return HttpResponse(...)

最佳答案

最上面的一个 (#1) 就是您想要的。

#2 只是将 HTTP 响应返回到原始函数。它向 View 本身返回 None。它不起作用并返回此错误。

enter image description here

关于django - 在 Django 的 View 函数中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58020799/

相关文章:

Django 测试 - 发送电子邮件失败

python - Django Rest 框架 - 外键上的 NOT NULL 约束

python - 通过循环通过 URL 传递参数

python - 为什么我使用 Tag.models.all() 后 Taggit (Django-Tag) 就无法工作

python - requests.post 从 python 脚本发送到使用 Apache 托管的 Django 网站,给出 403 Forbidden

服务器无法读取一个输入中的Django多个文件

Django:CSRF Coo​​kie 未在 iframe 中设置——没有 csrf 豁免的解决方法

python - 如何在forms.py和views.py中创建子类字段时更改父类中的字段?

python - 在 Django 中打开 zip 文件中存储的照片时出错

css - 我的更新页面的 css 没有正确加载,但其他页面工作得很好