在 Python/Django 中打印变量的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12848286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
print the value of a variable in Python/Django?
提问by Joren
In PHP I'm used to being able to do print $varor print_r($var).
在 PHP 中,我习惯于能够执行print $var或print_r($var).
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.
我可以在 python 的视图中打印变量,但它们最终出现在我的 cmd 窗口中,而不是页面本身。这对某些事情来说很好,但是当 Windows 的 cmd 窗口开始包装输出并将其变成乱码时,它并不是最易读的。
I tried doing {% print somevar %}in the django template, but I get TemplateSyntaxError: Invalid block tag: 'print'
我尝试{% print somevar %}在 django 模板中做,但我得到 TemplateSyntaxError: Invalid block tag: 'print'
So, for someone who's new to Python how do I easily see the value of a variable?
那么,对于 Python 新手,如何轻松查看变量的值?
采纳答案by Joran Beasley
<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__功能
回答by Scott Woodall
{{ your_variable }}will print the value.
{{ your_variable }}将打印值。
https://docs.djangoproject.com/en/dev/ref/templates/api/#basics
https://docs.djangoproject.com/en/dev/ref/templates/api/#basics
回答by Jorge Omar MH
You can try with:
您可以尝试:
${var}
and this is for print a variable
这是用于打印变量

