You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django类视图调用eBay Trading API报500错 控制台运行正常

问题背景

开发eBay商品上架模块时,eBay Trading API相关逻辑在控制台测试运行完全正常,但在Django类视图中调用相同函数时返回500服务器错误。
初步调试结论:

  • 代码可正常执行到addItemCheck函数内的print("Line 100")语句,函数调用链路正常
  • 传入函数的参数与控制台测试时完全一致
  • 故障点大概率出现在Trading API实例初始化环节

涉及代码

Django类视图代码

class ListSimpleProduct(generics.GenericAPIView, mixins.ListModelMixin, mixins.RetrieveModelMixin, mixins.CreateModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin): 

    queryset = SimpleProduct.objects.all()
    serializer_class = SimpleProductSerializer
    def post(self, request, pk): 
        product = SimpleProduct.objects.get(sku=pk)
        serializer = SimpleProductSerializer(product, many=False)
        data = serializer.data
        item = buildItem(data)
        addItemCheck(item)
        return Response({
            'data': "Item listed on ebay"
        })

eBay API调用函数代码

def addItemCheck(item):

    (opts, args) = init_options()
    try:
        print("Line 100")
        api = Trading(debug=opts.debug, config_file=opts.yaml, appid=opts.appid, domain=opts.domain,
                      certid=opts.certid, devid=opts.devid, warnings=True, timeout=20, siteid=opts.siteid)
        print("Line 103")
        response = api.execute('VerifyAddItem', item)
        print("Line 105")
        info = response.dict()
        print("Line 107")
        return info
    except ConnectionError as e:
        print(e)
        print(e.response.dict())
根因分析

故障核心是相对路径导致的配置文件读取失败:
控制台测试脚本通常在项目根目录执行,init_options()里配置的yaml文件路径是相对路径,进程可以正常定位文件完成SDK初始化;但Django运行时的工作目录和脚本执行目录不一致,SDK初始化时找不到对应配置文件,抛出未被捕获的异常触发500错误。
现有代码的异常捕获仅覆盖ConnectionError,初始化阶段的文件不存在、配置格式错误、文件权限不足这类异常完全没有被捕获,所以无法从现有日志拿到具体报错信息。

修复方案
  • 先补全异常捕获,打印完整错误栈确认具体问题
    调整异常捕获逻辑,覆盖初始化阶段可能抛出的所有异常,先拿到明确报错:
    import traceback
    def addItemCheck(item):
        (opts, args) = init_options()
        try:
            print("Line 100")
            api = Trading(debug=opts.debug, config_file=opts.yaml, appid=opts.appid, domain=opts.domain,
                          certid=opts.certid, devid=opts.devid, warnings=True, timeout=20, siteid=opts.siteid)
            print("Line 103")
            response = api.execute('VerifyAddItem', item)
            print("Line 105")
            info = response.dict()
            print("Line 107")
            return info
        except Exception as e:
            print(traceback.format_exc())
            if hasattr(e, 'response'):
                print(e.response.dict())
            raise
    
  • 替换配置文件为绝对路径,消除工作目录差异影响
    不要依赖运行时工作目录定位配置文件,基于Django的BASE_DIR拼接配置文件的绝对路径,示例:
    from django.conf import settings
    import os
    # 配置文件放在项目根目录,统一用绝对路径引用
    EBAY_YAML_PATH = os.path.join(settings.BASE_DIR, 'ebay_config.yaml')
    
    # 初始化SDK时传入绝对路径
    api = Trading(debug=opts.debug, config_file=EBAY_YAML_PATH, appid=opts.appid, domain=opts.domain,
                  certid=opts.certid, devid=opts.devid, warnings=True, timeout=20, siteid=opts.siteid)
    
  • 补充边界异常处理
    视图中SimpleProduct.objects.get(sku=pk)逻辑在匹配不到对应商品时会抛出DoesNotExist异常,同样会触发500,替换为get_object_or_404做合规处理;同时确认Django运行进程对配置文件有读取权限,避免权限类报错。

内容的提问来源于stack exchange,提问作者m_arbeiter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 19:09:22