Laravel在Django中的dd()等效项 [英] Laravel's dd() equivalent in django

查看:109
本文介绍了Laravel在Django中的dd()等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,很难弄清楚如何打印对象内部的内容.我的意思是变量的类型和值及其内部的成员.就像Laravel的dd(object)函数一样. Laravel的dd()是用于调试应用程序的便捷工具.

I am new in Django and having a hard time figuring out how to print what an object have inside. I mean type and value of the variable with its members inside. Just like Laravel's dd(object) function. Laravel's dd() is a handy tool for debugging the application.

我已经搜索了.但是发现没有什么用.我尝试了pprint()simplejsonprint(type(object){% debug %}.但是它们都不能提供有关对象的必需信息. 这是Laravel dd()函数的示例输出.

I have searched for it. But found nothing useful. I have tried pprint(), simplejson, print(type(object) and {% debug %}. But none of them could provide the required information about an object. Here is a sample output from Laravel's dd() function.

在此图像中,我正在打印Laravel的请求对象.如您所见,它显示了有关对象的完整信息.我的意思是它所属的类的名称,其成员变量.以及其类别名称和值.并不断深入对象内部并打印所有信息.

In this Image, I am printing the request object of Laravel. as you can see its showing complete information about an object. I mean name of class it belongs to, its member variables. also with its class name and value. and it keeps digging deep inside the object and prints all the information.

但是我无法在Django中找到类似的工具.很难相信Django没有这么有用的工具.因此,我想了解任何可以完成此操作的第三方程序包.

But I am not able to find a similar tool in Django. That's hard to believe that Django doesn't have such a useful tool. Therefore I would like to know about any third party package that can do the trick.

我正在使用Django 2.0.5版.并尝试打印django.contrib.messages

I am using Django version 2.0.5. and trying to print django.contrib.messages

此外,我希望输出显示在浏览器中,而不是在控制台中.以一种易于理解的方式,这就是为什么我要一个Django包,该包可以接收对象并呈现格式良好的对象体系结构表示形式.

Also, I want the output to be displayed in the browser, not in the console. In a well readable way that's why I am asking for a Django package that can take in the object and render a well-formed presentation of object architecture.

推荐答案

您正在寻找__dict__属性或dir()

print(object.__dict__)

pprint 用于美化输出

from pprint import pprint
pprint(dir(object))

这篇关于Laravel在Django中的dd()等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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