PyCharm:调试 Django Rest 视图集 [英] PyCharm: debug Django Rest Viewsets

查看:71
本文介绍了PyCharm:调试 Django Rest 视图集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下视图集(在 Rest Framework 文档中相同):

I have got the following viewset (same in the Rest Framework docs):

class UserViewSet(viewsets.ViewSet):

    def list(self, request):
        queryset = User.objects.all()
        serializer = UserSerializer(queryset, many=True)
        return Response(serializer.data)

假设这个视图集被调用:GET 127.0.0.1:8000/api/user

Let's say this viewset is called by: GET 127.0.0.1:8000/api/user

我想设置断点并观察当我从我的网络界面请求该视图集时代码的执行情况.但我不明白如何让我的断点起作用.

I want to set breakpoints and watch, how the code is executing when I request to that viewset from my web-interface. But I don't understand how to make my breakpoints to work.

我知道可以调试 django 命令(从 BaseCommand 继承并由 python manage.py 调用).我只是在 Run/Debug 配置 脚本 - manage.py 和脚本参数 - 中定义,一切都像魅力一样.

I know that it is possible to debug django commands (that are inherited from BaseCommand and called by python manage.py <command_name>). I simply define in Run/Debug configurations Script - manage.py and script parameters - <command_name> and everything works like a charm.

是否可以以某种方式调试我的视图集?当我调用我的视图集时,我希望我的代码在我的断点处停止.

Is it possible to Debug my viewsets somehow? I want my code to stop on my breakpoints when I call my viewset.

推荐答案

对于 Pycharm 专业人员:

For a Pycharm professional:

  1. 运行 - 编辑配置
  2. 点击+",选择Django服务器"
  3. 输入名称(Django调试服务器")、主机(127.0.0.1)和端口(8000),检查是否选择了正确的python解释器,点击确定.
  4. 运行 - 调试 - 选择您创建的调试配置Django 调试服务器".
  5. 根据您的情况,在 UserViewSet list() 方法中设置断点.可以肯定的是,在所有方面,在所有方面.
  6. 在任何浏览器中打开 127.0.0.1:8000/api/user.
  7. 调试器应该在您的断点处停止.

附言在 №5 中,您可以更深入地将断点设置为 django.core.handlers.base 方法 get_response() ,该方法处理对您的服务器的请求.

P.S. In №5 you can go deeper and set breakpoints to the django.core.handlers.base method get_response() that is handling requests to your server.

这篇关于PyCharm:调试 Django Rest 视图集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆