22

In PHP I'm used to being able to do print $var or 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.

I tried doing {% print somevar %} in the django template, but I get TemplateSyntaxError: Invalid block tag: 'print'

So, for someone who's new to Python how do I easily see the value of a variable?

Stedy
  • 7,359
  • 14
  • 57
  • 77
Joren
  • 9,623
  • 19
  • 63
  • 104

2 Answers2

31
<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

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Ah, I knew there'd be something so basic. There isn't by any chance a lib or something that can print large dicts in a more readable format with expanding/collapsing is there? – Joren Oct 11 '12 at 20:54
  • not off hand ... its a good idea though there may be a template tag for it somewhere on the interwebs – Joran Beasley Oct 11 '12 at 20:58
9

{{ your_variable }} will print the value.

https://docs.djangoproject.com/en/dev/ref/templates/api/#basics

Scott Woodall
  • 10,456
  • 6
  • 39
  • 37