How to print query result in python/django
<p>I came from CakePHP and just started playing with Django framework. In CakePHP, I have the habit of printing out all the returned array using pr() straight out to the webpage. For example:</p>

<ul>
<li>A controller spits out a $result to a View, I use pr($result) and it will print out everything right on the webpage so I know how to travel through $result from my View.</li>
<li>A form POST a $request to a Controller, I use pr($request) to see what is sending in before processing it in the Controller. The content of $request will be displayed immediately on the webpage right after I hit Submit the form.</li>
</ul>

<p>I'm wondering if I could do the same thing in django instead of going to the shell and try pprint (or could I just use pprint to print out to the web???)
This is a really simple example about what I'm talking about:
<strong><em>app_name/views.py:</em></strong></p>

<pre><code>def detail(request, soc_id):
    soc = get_object_or_404(Soc, pk=soc_id)
    return render(request, 'socs/detail.html', {'soc': soc})
</code></pre>

<p>How can I just view clearly what is in "soc". In cakephp, I could just pr($soc) right there and it will be displayed right on the detail.html page.</p>

<p>I have tried this and it didn't work (I'm sure it's basic but i'm just new to this)</p>

<pre><code>import pprint
def detail(request, soc_id):
    soc = get_object_or_404(Soc, pk=soc_id)
    pprint.pprint(soc)
    return render(request, 'socs/detail.html', {'soc': soc})
</code></pre>

<p>I have spent two days doing research but I couldn't find the answer. I'm hoping one of you can help this newbie out.</p>

<p>I meant something like this in CakePHP</p>

<pre><code>/* First, create our posts table: */
CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

/* Then insert some posts for testing: */
INSERT INTO posts (title, body, created)
    VALUES ('The title', 'This is the post body.', NOW());
INSERT INTO posts (title, body, created)
    VALUES ('A title once again', 'And the post body follows.', NOW());
INSERT INTO posts (title, body, created)
    VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());
</code></pre>

<p>And in the PostController.php</p>

<pre><code>class PostsController extends AppController {
    public $helpers = array('Html', 'Form');

    public function index() {
        $this-&gt;set('posts', $this-&gt;Post-&gt;find('all'));
    }
}
</code></pre>

<p>And with the variable $posts in $this->set('posts'...) I can do this</p>

<pre><code>// print_r($posts) output:

Array
(
    [0] =&gt; Array
        (
            [Post] =&gt; Array
                (
                    [id] =&gt; 1
                    [title] =&gt; The title
                    [body] =&gt; This is the post body.
                    [created] =&gt; 2008-02-13 18:34:55
                    [modified] =&gt;
                )
        )
    [1] =&gt; Array
        (
            [Post] =&gt; Array
                (
                    [id] =&gt; 2
                    [title] =&gt; A title once again
                    [body] =&gt; And the post body follows.
                    [created] =&gt; 2008-02-13 18:34:56
                    [modified] =&gt;
                )
        )
    [2] =&gt; Array
        (
            [Post] =&gt; Array
                (
                    [id] =&gt; 3
                    [title] =&gt; Title strikes back
                    [body] =&gt; This is really exciting! Not.
                    [created] =&gt; 2008-02-13 18:34:57
                    [modified] =&gt;
                )
        )
)
</code></pre>

<p>The output will be displayed directly on the Index page of Posts. Really easy for me to travel through the $posts variable to actually write the view.</p>

<p>I'm looking for something similar in Django. I really hope I explain my question clear enough, since I don't have enough experience with Django yet.</p>

php python django cakephp pprint