gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:17:50 26 4
gpt4 key购买 nike

我正在使用 Django (2, 2, 4, 'final', 0)在 docker 中,但我可以在里面猛击以打开或执行所需的任何内容。但是我不能调试。 ( How to debug in Django, the good way? 陈述了一些方法,没有对我有用)

在我的 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() - 两者都返回相同的结果。
到目前为止,在我的 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 并 telnet 进入 pdb session :
telnet 0.0.0.0 4444

关于python - 无法在 Django 中调试或使用 pdb : bdb. BdbQuit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58491870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com