Easier debugging of Django templates. {% try %} ... {% endtry %} template tag and traceback.

1,420 views
Skip to first unread message

Jonathan S

unread,
Mar 5, 2010, 11:09:47 AM3/5/10
to Django developers
Hi Django users,

We made at our company, City Live, a template tag you may be
interested in.
It's a try/catch statement. When anything in the try block fails, a
traceback through all the included templates is shown.

A good approach would be to include {% try %} ... {% endtry %} in the
base template of the whole site, so that any template rendering error
will be caught.

If you prefer a custom message instead of the traceback, the following
is also possible:
{% try %} ... {% except %} custom error message {% endtry %}


Screenshot of the traceback, embedded in our site:
http://github.com/citylive/Django-Template-Tags/blob/master/screenshots/traceback.png

Source:
http://github.com/citylive/Django-Template-Tags/blob/master/templatetags/debug.py

It depends on this {% stylize %} template tag and pygments for the
highlighting.
http://github.com/citylive/Django-Template-Tags/blob/master/templatetags/stylize.py

All feedback is welcome!

Jonathan

Jared Forsyth

unread,
Mar 5, 2010, 11:49:27 AM3/5/10
to django-d...@googlegroups.com
I presume these only get shown with DEBUG turned on?


--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.


Jonathan S

unread,
Mar 5, 2010, 11:53:11 AM3/5/10
to Django developers

On Mar 5, 5:49 pm, Jared Forsyth <ja...@jaredforsyth.com> wrote:
> I presume these only get shown with DEBUG turned on?

Yes, they are only shown when DEBUG is on:

see line 163:
http://github.com/citylive/Django-Template-Tags/blob/master/templatetags/debug.py#L163


Jared Forsyth

unread,
Mar 5, 2010, 12:59:08 PM3/5/10
to django-d...@googlegroups.com
On a related note, is there a way to tell templates *not* to fail silently? (most tags etc. do)
for testing/debugging purposes?

jared



--

Jonathan S

unread,
Mar 5, 2010, 1:11:07 PM3/5/10
to Django developers

On Mar 5, 6:59 pm, Jared Forsyth <ja...@jaredforsyth.com> wrote:
> On a related note, is there a way to tell templates *not* to fail silently?
> (most tags etc. do)
> for testing/debugging purposes?

I don't think that all template tags fail silently.

Lookup of variables does always fail silently. That is, when a
variable is not found in the context, it will automatically evaluate
to None. So {{ non_existing_var }} will do nothing, but also {% if
non_existing_var %} ... {% endif %} won't do anything.

{% url name param %}, for instance won't fail silently when reverse
doesn't find a match.
Also, all the template tags that I write for my own use won't fail
silently, because I dislike that behavior.

Probably, if we don't want template tags to fail silently, all we have
to do, is patch the resolve call of variables which is done in most
template tags during the rendering.
Variable('varname').resolve(context)

I will look into this. It should be easy to change that behavior. (and
possibly allow {% try %} ... {% endtry %} catch the variable resolve
error.)

Leo

unread,
Mar 5, 2010, 9:36:26 PM3/5/10
to Django developers
This is really cool, thanks for sharing it!

One small question though, would it be better to check TEMPLATE_DEBUG
instead of DEBUG - http://docs.djangoproject.com/en/dev/ref/settings/#template-debug
?

--
--Leo

Jonathan S

unread,
Mar 6, 2010, 4:59:01 AM3/6/10
to Django developers

On Mar 6, 3:36 am, Leo <leo.shklovs...@gmail.com> wrote:
> This is really cool, thanks for sharing it!
>
> One small question though, would it be better to check TEMPLATE_DEBUG
> instead of DEBUG -http://docs.djangoproject.com/en/dev/ref/settings/#template-debug
> ?


Yes, TEMPLATE_DEBUG would be better indeed. Thanks. I'll change that.

Now, I'm also planning to show the locals() for each frame. (Similar
to the original django traceback)

Russell Keith-Magee

unread,
Mar 6, 2010, 7:47:39 AM3/6/10
to django-d...@googlegroups.com

Hi Jonathan,

This looks really interesting. Is this something that could be
integrated into Django's template page itself? It strikes me that this
sort of feedback would be a good addition to Django's own debug page,
rather than requiring end users to pepper debug tools like try-catch
block in their template code.

Yours
Russ Magee %-)

Jonathan S

unread,
Mar 6, 2010, 8:00:47 AM3/6/10
to Django developers

> This looks really interesting. Is this something that could be
> integrated into Django's template page itself? It strikes me that this
> sort of feedback would be a good addition to Django's own debug page,
> rather than requiring end users to pepper debug tools like try-catch
> block in their template code.

Hi Russ,

You mean, whether this could replace the traceback from Django without
requiring you to have a {% try %} block in the template?
I'm not 100% sure, but it should be possible to design middleware
which will catch any exception if TEMPLATE_DEBUG and DEBUG are True,
and return this traceback instead.

Certainly something I will take a look at next week.

Jonathan

Reply all
Reply to author
Forward
Message has been deleted
0 new messages