如何在 AJAX 调用上使用 django-debug-toolbar?

发布于 2024-10-19 07:42:09 字数 231 浏览 1 评论 0 原文

我很好奇是否有一种合理的方法可以将(令人惊叹的)django-debug-toolbar 与 AJAX 查询一起使用。

例如,我使用带有一堆参数的 jQuery $.get 来访问 Django URL 并内联加载它。如果我遇到错误,它不会在工具栏上注册。我也无法通过复制 AJAX URL 来使用它,因为 DDT 附加到响应的正文标记,并且在 AJAX 响应中包含正文标记没有任何意义。

任何方向都会有帮助!谢谢!

I'm curious if there's a reasonable way to use the (amazing) django-debug-toolbar with AJAX queries.

For example, I use a jQuery $.get with a bunch of parameters to hit a Django URL and load it inline. If I have an error with that, it isn't registered on the toolbar. I also can't use it by copying the AJAX URL because DDT attaches to the body tag of the response, and it wouldn't make any sense to be including body tags with AJAX responses.

Any direction would be helpful! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

捎一片雪花 2024-10-26 07:42:09

更新:此功能现已内置于 django-debug-toolbar 中,

已添加在此 PR 中 并在版本 3.0 中发布

旧答案对于旧版本的工具栏可能仍然有用:

我编写了 Django 调试工具栏的请求历史记录面板,可以添加到 Django 调试工具栏以查看当前请求以外的请求(包括 AJAX 请求)。

通过 pip 安装:

pip install django-debug-toolbar-request-history

settings.py 中将 'ddt_request_history.panels.request_history.RequestHistoryPanel' 添加到 DEBUG_TOOLBAR_​​PANELS 例如:

DEBUG_TOOLBAR_PANELS = [
    'ddt_request_history.panels.request_history.RequestHistoryPanel',  # Here it is 
    'debug_toolbar.panels.versions.VersionsPanel',
    'debug_toolbar.panels.timer.TimerPanel',
    'debug_toolbar.panels.settings.SettingsPanel',
    'debug_toolbar.panels.headers.HeadersPanel',
    'debug_toolbar.panels.request.RequestPanel',
    'debug_toolbar.panels.sql.SQLPanel',
    'debug_toolbar.panels.templates.TemplatesPanel',
    'debug_toolbar.panels.staticfiles.StaticFilesPanel',
    'debug_toolbar.panels.cache.CachePanel',
    'debug_toolbar.panels.signals.SignalsPanel',
    'debug_toolbar.panels.logging.LoggingPanel',
    'debug_toolbar.panels.redirects.RedirectsPanel',
    'debug_toolbar.panels.profiling.ProfilingPanel',
]

Update: this functionality is now built into django-debug-toolbar

It was added in this PR and released in version 3.0

The old answer may still be useful for older versions of the toolbar:

I wrote the Request History Panel for Django Debug Toolbar that can be added to the Django Debug Toolbar to view requests other than the current one (including AJAX requests).

Install via pip:

pip install django-debug-toolbar-request-history

In settings.py add 'ddt_request_history.panels.request_history.RequestHistoryPanel' to DEBUG_TOOLBAR_PANELS e.g.:

DEBUG_TOOLBAR_PANELS = [
    'ddt_request_history.panels.request_history.RequestHistoryPanel',  # Here it is 
    'debug_toolbar.panels.versions.VersionsPanel',
    'debug_toolbar.panels.timer.TimerPanel',
    'debug_toolbar.panels.settings.SettingsPanel',
    'debug_toolbar.panels.headers.HeadersPanel',
    'debug_toolbar.panels.request.RequestPanel',
    'debug_toolbar.panels.sql.SQLPanel',
    'debug_toolbar.panels.templates.TemplatesPanel',
    'debug_toolbar.panels.staticfiles.StaticFilesPanel',
    'debug_toolbar.panels.cache.CachePanel',
    'debug_toolbar.panels.signals.SignalsPanel',
    'debug_toolbar.panels.logging.LoggingPanel',
    'debug_toolbar.panels.redirects.RedirectsPanel',
    'debug_toolbar.panels.profiling.ProfilingPanel',
]
赢得她心 2024-10-26 07:42:09

我以前也遇到过同样的问题!
随着我做越来越多的 AJAX 重型应用程序,我发布了 Django 应用程序Chrome 扩展程序共同解决了这个问题。

所有信息都在 github 存储库中。

I had the same problem before!
And as I'm doing more and more AJAX heavy applications, I released a Django Application and a Chrome extension that together solved exactly that problem.

All the information is in the github repository.

说不完的你爱 2024-10-26 07:42:09

我最近遇到了这个问题。我的快速但肮脏但有效的解决方案只是添加一些 HTML 视图来弯曲相同的代码。

例如,如果我在 NewRelic 中看到我的网站 90% 的时间都花费在对 /search_for_book?title= 的 ajax 调用上,那么我的代码可能如下所示:

views.py:

def search_for_book(request, title):
    data = _search_for_book(title)
    return json_response(data)

def test_search_for_book(request, title):
    data = _search_for_book(title)
    return http_response(data)

瓶颈将位于 _search_for_book代码;我们是否通过 ajax 调用它与诊断其低效率无关(至少就我而言;YMMV)

I've hit this problem recently. My quick-n-dirty-but-working solution was just to add some HTML views to flex the same code.

So for example, if I can see in NewRelic that 90% of my website's time is spent in an ajax call to /search_for_book?title=, my code might look like this:

views.py:

def search_for_book(request, title):
    data = _search_for_book(title)
    return json_response(data)

def test_search_for_book(request, title):
    data = _search_for_book(title)
    return http_response(data)

The bottleneck will be somewhere in the _search_for_book code; whether we call it by ajax is irrelevant to diagnosing its inefficiencies (in my case, at least; YMMV)

白龙吟 2024-10-26 07:42:09

Ddt 将自身插入响应中,这意味着没有标准方法可以浏览其面板以获取 AJAX 请求。此外,AJAX 响应可以是 JSON 格式,这使得 ddt 无法插入其中。

就我个人而言,我会找到一种将 ddt 输出记录到文本文件的方法,或者它可能支持客户端-服务器架构,其中客户端在 AJAX 请求处理程序内工作并将数据发送到服务器?我不知道有什么可能,因为那里有十几个 ddt 克隆。

Ddt plugs itself into a response, which means that there is no standard way of browsing its panels for AJAX request. Also, AJAX response can be in JSON format, which makes it impossible for ddt to plug into it.

Personally I'd find a way of logging ddt output to a text file, or maybe it supports client-server architecture in which client works inside AJAX request handler and sends data to the server? I don't know what's possible as there are dozen ddt clones out there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文