print_r() in Django & SQL debug output

963 views
Skip to first unread message

Okto Silaban

unread,
May 2, 2009, 7:53:49 PM5/2/09
to django...@googlegroups.com
Ok, I know this is not a PHP mailing list.. I just want to make the question simpler.

I'm coming from CakePHP background.

1. What Django template tags can I use to replace print_r() / var_dump() in PHP?
2. How can I display SQL debug output?

*in cakePHP I can use debug() function to show the SQL debug. So I can see what SQL query it runs

debug output example :

SELECT * FROM myapp_entry


Sorry for my English..

regards,
okto.silaban.net

George Song

unread,
May 2, 2009, 8:04:47 PM5/2/09
to django...@googlegroups.com
On 5/2/2009 4:53 PM, Okto Silaban wrote:
> Ok, I know this is not a PHP mailing list.. I just want to make the
> question simpler.
>
> I'm coming from CakePHP background.
>
> 1. What Django template tags can I use to replace print_r() / var_dump()
> in PHP?
> 2. How can I display SQL debug output?
>
> *in cakePHP I can use debug() function to show the SQL debug. So I can
> see what SQL query it runs
>
> debug output example :
>
> SELECT * FROM myapp_entry

Django has extensive documentation. It's advisable that you look it up
first before asking:

<http://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-debug>

Okto Silaban

unread,
May 2, 2009, 8:47:29 PM5/2/09
to django...@googlegroups.com
On Sun, May 3, 2009 at 7:04 AM, George Song <geo...@damacy.net> wrote:

Django has extensive documentation. It's advisable that you look it up
first before asking:

<http://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-debug>

Default :
DEBUG = True
TEMPLATE_DEBUG = DEBUG

And I've added this in settings.py :

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n"
)

INTERNAL_IPS = ('127.0.0.1',)

And added these lines in my template file  :

<div id="debug">
  <h2>Queries</h2>
  <p>
    {{ sql_queries|length }} Quer{{ sql_queries|pluralize:"y,ies" }}
    {% ifnotequal sql_queries|length 0 %}
    (<span style="cursor: pointer;" onclick="var s=document.getElementById('debugQueryTable').style;s.display=s.display=='none'?'':'none';this.innerHTML=this.innerHTML=='Show'?'Hide':'Show';">Show</span>)
    {% endifnotequal %}
  </p>
  <table id="debugQueryTable" style="display: none;">
    <col width="1"></col>
    <col></col>
    <col width="1"></col>
    <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">SQL</th>
      <th scope="col">Time</th>
    </tr>
    </thead>
    <tbody>
    {% for query in sql_queries %}<tr class="{% cycle odd,even %}">
      <td>{{ forloop.counter }}</td>
      <td>{{ query.sql|escape }}</td>
      <td>{{ query.time }}</td>
    </tr>{% endfor %}
    </tbody>
  </table>
</div>

This is the output :
Queries
0 Query (Show)

Meanwhile, it does do query.. As these lines show correct DB output :

<!--loop-->
    {% for item in items %}                
       
    <p class="punar">
        <b>{{item.nominal}}</b> :
        <a href="/punar/items/category/4">            
            {{ item.category.name }}
        </a> -
        {{item.info}}
        {% if item.location.location %}
        @{{ item.location.location }}
        {% endif %}
        <span class="waktu">[3:53]</span>

        <a href="/punar/items/delete/85">[x]</a>       
    </p>
   

     {% endfor %}        
<!--/loop-->

regards,
Okto.silaban.net

Malcolm Tredinnick

unread,
May 2, 2009, 10:27:27 PM5/2/09
to django...@googlegroups.com

How are you rendering that template? The only thing I can think of that
will go wrong here is if you do not pass a RequestContext to
render_to_response() -- or whatever rendering method you are using.
Context processors only run if the context is a RequestContext, not
simply a Context object.

See this documentation (esp. the "note" callout):
http://docs.djangoproject.com/en/dev/ref/templates/api/#id1

Regards,
Malcolm


Okto Silaban

unread,
May 2, 2009, 10:48:59 PM5/2/09
to django...@googlegroups.com
On Sun, May 3, 2009 at 9:27 AM, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:

How are you rendering that template? The only thing I can think of that
will go wrong here is if you do not pass a RequestContext to
render_to_response() -- or whatever rendering method you are using.
Context processors only run if the context is a RequestContext, not
simply a Context object.

See this documentation (esp. the "note" callout):
http://docs.djangoproject.com/en/dev/ref/templates/api/#id1

Thanx.. You're right, I forgot to add this as the third parameter : context_instance=RequestContext(request)

And, how about my first question ?

1. What Django template tags can I use to replace print_r() / var_dump() in PHP?

regards,
okto.silaban.net




Malcolm Tredinnick

unread,
May 2, 2009, 11:02:05 PM5/2/09
to django...@googlegroups.com

There are no direct equivalents, because Django templates are at a
different level and provide different functionality to PHP files.
However, there are a few things you can do for viewing the context and
seeing things in "pretty printed" format. Here are a bunch of ideas I
wrote up recently:
http://www.pointy-stick.com/blog/2009/02/03/some-simple-django-debugging-tools/

Regards,
Malcolm


Dan Mallinger

unread,
May 3, 2009, 2:52:23 AM5/3/09
to django...@googlegroups.com
Hi Okto,

I feel confident saying that Malcolm knows the ins and outs of Django
far better than I do :) But, for what it's worth, I really like the
logging package here:
http://code.google.com/p/django-logging/wiki/Overview
It's super easy and reasonably powerful. And if you install Sphinx,
it'll look more beautiful than anything that ever comes out of
CakePHP.
I'll also take this moment to say: As someone who's worked with
CakePHP quite a bit, Django is a wonderful, wonderful change!

Cheers to Malcolm and everyone else on the team,
Dan
Reply all
Reply to author
Forward
0 new messages