简体   繁体   English

在Python / Django中打印变量的值?

[英]print the value of a variable in Python/Django?

In PHP I'm used to being able to do print $var or print_r($var) . 在PHP中,我习惯于能够print $varprint_r($var)

I can print variables in my views in python but they end up in my cmd window not on the page itself. 我可以在python中的视图中打印变量,但它们最终出现在我的cmd窗口而不是页面本身。 This is fine for some things but windows' cmd window isn't exactly the most readable when it starts wrapping output and turning it into gibberish. 这对于某些事情来说很好,但是当它开始包装输出并将其变成乱码时,Windows的cmd窗口并不是最可读的。

I tried doing {% print somevar %} in the django template, but I get TemplateSyntaxError: Invalid block tag: 'print' 我尝试在django模板中执行{% print somevar %} ,但我得到了TemplateSyntaxError:无效的块标记:'print'

So, for someone who's new to Python how do I easily see the value of a variable? 那么,对于那些不熟悉Python的人,我如何轻松地看到变量的值?

<html>
{{ this_is_my_variable_that_is_passed_to_my_view }}
</html>

the {{ allow variable access to the item which will call the items __unicode__ function if it exists otherwise it will call its __str__ function {{允许变量访问将调用项__unicode__函数的项,如果它存在,否则它将调用其__str__函数

You can try with: 您可以尝试:

${var}

and this is for print a variable 这是用于打印变量

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

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