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?

1
  • 2
    Why are you doing this? Is it for debugging, or is it just to use the value of a variable in the template? Commented Oct 11, 2012 at 20:50

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
not off hand ... its a good idea though there may be a template tag for it somewhere on the interwebs
9

{{ your_variable }} will print the value.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.