【问题标题】:How to check if a function returns response object如何检查函数是否返回响应对象
【发布时间】:2014-07-07 10:41:41
【问题描述】:

我有一个函数调用另一个函数并继续调用函数的结果。

我要做的是检查被调用的函数是否返回response 对象,然后在现有函数中使用它。

我尝试使用 isinstance,但没有得到要使用的 arg。

def func(request):
    return HttpResponse('xyz')

def check_func(request):

    res = func()
    # Here I want to check if res is response object or not
    # And continue accordingly

建议一些检查方法。

【问题讨论】:

    标签: python django response


    【解决方案1】:

    您可以测试它是否是HttpResponse 的实例,它是所有响应类型的base class。

    from django.http import HttpResponse
    
    def check_func(request):
    
        res = func()
        if isinstance(res, HttpResponse):
           #do something
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 2017-09-10
      相关资源
      最近更新 更多