views:

150

answers:

2

http://www.djangobook.com/en/beta/chapter10/

"Note that django.core.exceptions.ObjectDoesNotExist, which is the base class for all Django database API DoesNotExist exceptions, has silent_variable_failure = True. So if you’re using Django templates with Django model objects, any DoesNotExist exception will fail silently."

Whilst I'm developing I'd like to reverse this behaviour, i.e. silent_variable_failure = False. How do I make this change permanent whilst developing under django ?

Thanks, Nick

A: 

The Django template system looks for silent_variable_failure as an attribute of any exception.

This means you have do define your own exception with that attribute set to False.

For the standard ObjectDoesNotExist exception generated by the ORM, etc., the simplest solution would be to modify the Django sourcecode on your development box (django/core/exceptions.py).

If this was something you needed to change on a production system then you would need to 1) define your own exception class, 2) put a manager on your model that is called from the template (instead of using ORM methods directly), and then 3) catch DoesNotExist in your manager and re-raise the exception with your own class.

Van Gale
Thanks for the good info. I edited /usr/share/python-support/python-django/django/core/exceptions.py and changed to silent_variable_failure = False but i could not get the behaviour to change. Not sure if I needed to regenerate .pyc file. I deleted /var/lib/python-support/python2.6/django/core/exceptions.pyc in desperation but to no avail.I think TEMPLATE_STRING_IF_INVALID will do what I want, maybe the behaviour is different in later version of django. I'm running Django version 1.0.2 final
+3  A: 

In settings.py I added TEMPLATE_STRING_IF_INVALID = "invalid string '%s'" See http://docs.djangoproject.com/en/dev/ref/templates/api/#invalid-template-variables for more info.

It includes this warning "Many templates, including those in the Admin site, rely upon the silence of the template system when a non-existent variable is encountered. If you assign a value other than '' to TEMPLATE_STRING_IF_INVALID, you will experience rendering problems with these templates and sites."