在Python / Django中打印一个变量的值? [英] print the value of a variable in Python/Django?
问题描述
在PHP中,我习惯于能够执行 print $ var
或 print_r($ var)
。
In PHP I'm used to being able to do print $var
or print_r($var)
.
我可以在我的视图中打印python中的变量,但是它们不在页面本身的cmd窗口中。这对某些东西是好的,但Windows的cmd窗口不是最可读的,当它开始包装输出并将其变成乱码。
I can print variables in my views in python but they end up in my cmd window not on the page itself. 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.
我尝试过 {%print somevar%}
在django模板中,但是我得到TemplateSyntaxError:无效的块标签:'print'
I tried doing {% print somevar %}
in the django template, but I get TemplateSyntaxError: Invalid block tag: 'print'
对于Python的新手,我如何轻松地看到变量的值?
So, for someone who's new to Python how do I easily see the value of a variable?
推荐答案
<html>
{{ this_is_my_variable_that_is_passed_to_my_view }}
</html>
{{
允许变量访问项目将调用项目 __ unicode __
函数,否则它将调用它的 __ str __
函数
the {{
allow variable access to the item which will call the items __unicode__
function if it exists otherwise it will call its __str__
function
这篇关于在Python / Django中打印一个变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!