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
--
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.
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
--
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.)
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
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)
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 %-)
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