无法在 Django 中调试或使用 pdb:bdb.BdbQuit

Unable to debug or use pdb in Django: bdb.BdbQuit

我在 docker 中使用 Django (2, 2, 4, 'final', 0),但我能够在 bash 中打开或执行所需的任何内容。但是我无法调试。 (How to debug in Django, the good way? 陈述了一些方法,none 对我有用)

在我的 views.py 中,我有各种功能,例如这里的这个。

def visGraph(request):
    showgraph = 'Graphen'
    selectDB = request.GET.get('selectDB', '')
    __import__("pdb").set_trace()
    title += " <i>"+showgraph+"</i> ("+selectDB+")"

在我填写 pdb 之前它工作正常,添加调试器会使我的应用程序立即崩溃:

> /code/DjangoGraphen/views.py(74)visGraph()
-> title += " <i>"+showgraph+"</i> ("+selectDB+")"
(Pdb) 
Internal Server Error: /DjangoGraphen/visGraph
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "./DjangoGraphen/views.py", line 74, in visGraph
    title += " <i>"+showgraph+"</i> ("+selectDB+")"
  File "./DjangoGraphen/views.py", line 74, in visGraph
    title += " <i>"+showgraph+"</i> ("+selectDB+")"
  File "/usr/lib64/python3.7/bdb.py", line 88, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib64/python3.7/bdb.py", line 113, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit
ERROR:django.request:Internal Server Error: /DjangoGraphen/visGraph
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "./DjangoGraphen/views.py", line 74, in visGraph
    title += " <i>"+showgraph+"</i> ("+selectDB+")"
  File "./DjangoGraphen/views.py", line 74, in visGraph
    title += " <i>"+showgraph+"</i> ("+selectDB+")"
  File "/usr/lib64/python3.7/bdb.py", line 88, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib64/python3.7/bdb.py", line 113, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit
[21/Oct/2019 17:47:14] "GET /DjangoGraphen/visGraph?selectDB=Test&showgraph=graph HTTP/1.1" 500 88178

如果我使用 __import__("pdb").set_trace()breakpoint() 并不重要 - 两者 return 相同的结果。 在我的 settings.py 中,到目前为止 DEBUG = True,将其设置为 False 不会改变任何东西。

我正在我的命令行中使用以下命令查看日志:

docker logs django_web_1 -f

我假设对于 pdb 我需要一个活动的 shell 而不仅仅是一个日志查看器,但我不知道要更改什么或如何更改。 但是已经尝试过这里给出的答案:Interactive shell in Django 但它只是打开一个 Python-Shell.

解决方法其实很简单。问题是docker。该解决方案在此处说明并有效: https://medium.com/@vladyslav.krylasov/how-to-use-pdb-inside-a-docker-container-eeb230de4d11

将此添加到您的 docker-compose.yml:

 ports:
      - "4444:4444"
    stdin_open: true
    tty: true

安装 remote-pdb 并使用而不是默认的 pdb 命令:

__import__("remote_pdb").set_trace(host='0.0.0.0', port=4444)

登录到您的 docker 并远程登录到 pdb 会话:

telnet 0.0.0.0 4444

如果你知道你将在调试器中结束,你可以使用 run 而不是 up

$ docker-compose run --rm --service-ports django_web

问题是 up 假设它会 运行 多项服务,即使你告诉它只提供 运行 一项服务,所以它会为你包装它们。这也是它在输出前加上服务名称的原因:

web_1  | Some output
db_1   | Some db logs

run 命令 doesn't do this 等你可以有一个 shell 和一个没有问题的调试器或 remote_pdb 解决方法。

注意:使用 run 时,您必须 configure the dependencies 因为并非所有内容都会自动启动。