Home:ALL Converter>Django: How to get template debug info to user on syntaxerror

Django: How to get template debug info to user on syntaxerror

Ask Time:2013-01-26T20:51:32         Author:Allard Stijnman

Json Formatter

I have a page where users can submit their own template into a textarea, for now it needs to be that simple. However since it is user generated input i need to verify they don't do stuff that breaks the rest of my application and simultaneously giving useful feedback as to what they did wrong if they did something wrong.

For the purpose of giving useful feedback i want something similair looking to what django supplies me with (using django 1.4):

enter image description here

The above bit in particular. I'm putting the user template in a django template so i don't have to verify syntax errors and stuff myself. That looks something like this:

try:
    template = get_template_from_string(user_input)
    template.render(context=Context())
except:
    do something to ouptut the error

The render call is needed otherwise not exceptions are thrown at all.

I have tried printing the exception and it's arguments, but that only gives very limited info. I also tried using traceback but that never gives back line numbers or anything within the template, only to where the exception is thrown in the python code. I also couldn't find anything using Google and my search through Django source code have left me wondering where the actual error page is generated...


So basically my question is; how do i get the bit of information shown in the image?

EDIT To clarify: I want users to be able to make templates so these templates can be used while sending emails. Since the Django templating engine is already present I figured i would use that one instead of something else.

The templates themselves are made safe using this snippet and some parsing of my own for the variables. Everything works so far except the useful debug message to the user. So far all I've got on that is: "Something went wrong while parsing your template, unexpected block tag extends" (for example), which I would like to be more like the image shown above.

Author:Allard Stijnman,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14537097/django-how-to-get-template-debug-info-to-user-on-syntaxerror
yy