简体   繁体   English

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

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

I'm using Django (2, 2, 4, 'final', 0) within a docker, but I'm able to bash inside to open or execute whatever is required.我在 docker 中使用 Django (2, 2, 4, 'final', 0) ,但我能够在 bash 内部打开或执行任何需要的操作。 But I can't debug.但我无法调试。 ( How to debug in Django, the good way? states some methods, none work for me) 如何在 Django 中调试,好方法?说明一些方法,对我没有用)

Within my views.py I'm having various functions, for instance this here.在我的views.py中,我有各种功能,例如这里。

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

It works fine until I fill in the pdb, adding the debugger makes my app crash immediately:在我填写 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

It doesn't matter very much if I'm using __import__("pdb").set_trace() or breakpoint() - both return the same result.如果我使用__import__("pdb").set_trace()breakpoint()并不重要 - 两者都返回相同的结果。 In my settings.py I have so far DEBUG = True , setting it to False doesn't change anything.在我的 settings.py 中,到目前为止DEBUG = True ,将其设置为False不会改变任何东西。

I'm viewing the logs within my command line using:我正在使用以下命令在命令行中查看日志:

docker logs django_web_1 -f

I assume for the pdb I require an active shell rather than just a log-viewer, but I can't figure out what to change or how to do that.我假设对于 pdb,我需要一个活动的 shell 而不仅仅是一个日志查看器,但我不知道要更改什么或如何做到这一点。 But tried already what's given here as an answer: Interactive shell in Django But it just opens a Python-Shell.但是已经尝试过这里给出的答案: Interactive shell in Django但它只是打开了一个 Python-Shell。

The solution is actually rather easy.解决方案实际上相当简单。 The problem is the docker.问题是 docker。 The solution is stated here and works: https://medium.com/@vladyslav.krylasov/how-to-use-pdb-inside-a-docker-container-eeb230de4d11解决方案在这里说明并且有效: https://medium.com/@vladyslav.krylasov/how-to-use-pdb-inside-a-docker-container-eeb230de4d11

Add this to your docker-compose.yml:将此添加到您的 docker-compose.yml:

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

Install the remote-pdb and use instead of your default pdb command:安装remote-pdb并使用而不是默认的 pdb 命令:

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

Log into your docker and telnet into the pdb session:登录到您的 docker 并远程登录到 pdb session:

telnet 0.0.0.0 4444

If you know you are going to end up in the debugger you can use run instead of up .如果你知道你最终会进入调试器,你可以使用run而不是up

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

The problem is that up assumes it will run multiple services, even when you tell it to only run one, so it will wrap them for you.问题是up假设它会运行多个服务,即使你告诉它只运行一个,所以它会为你包装它们。 That's also why it prepends the output with the service name:这也是它在 output 前面加上服务名称的原因:

web_1  | Some output
db_1   | Some db logs

The run command doesn't do this and so you can have a shell and a debugger without problems or remote_pdb workarounds. run命令不会执行此操作,因此您可以拥有一个 shell 和一个没有问题的调试器或 remote_pdb 解决方法。

NOTE: when using run , you have to configure the dependencies because not everything is automatically started otherwise.注意:使用run时,您必须配置依赖项,否则并非所有内容都会自动启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM