From 5f8f00d21eae8ce67c3a81d16d6ab3db13f11049 Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Tue, 8 Sep 2009 10:58:07 +0200 Subject: [PATCH] The templatetag will raise an exception if a related object is not found using the "into" argument. It will fail silently if settings.DEBUG is False. --- .../templatetags/generic_flatblocks.py | 2 ++ docs/ref/usage.rst | 8 +++++++- example_project/templates/500.html | 1 + example_project/templates/example.html | 15 +++++---------- 4 files changed, 15 insertions(+), 11 deletions(-) create mode 100644 example_project/templates/500.html diff --git a/django_generic_flatblocks/templatetags/generic_flatblocks.py b/django_generic_flatblocks/templatetags/generic_flatblocks.py index fddf81c..acf639d 100644 --- a/django_generic_flatblocks/templatetags/generic_flatblocks.py +++ b/django_generic_flatblocks/templatetags/generic_flatblocks.py @@ -55,6 +55,8 @@ def get_content_object(self, related_model, slug): related_object = related_model._default_manager.get(pk=slug) return None, related_object except related_model.DoesNotExist: + if settings.DEBUG: + raise related_object = related_model() return None, related_object diff --git a/docs/ref/usage.rst b/docs/ref/usage.rst index d26b557..7c44798 100644 --- a/docs/ref/usage.rst +++ b/docs/ref/usage.rst @@ -97,4 +97,10 @@ directly without creating a template:: would be rendered as:: - The first user is johndoe! (edit) \ No newline at end of file + The first user is johndoe! (edit) + +.. note:: + If you have `settings.DEBUG` set to `True` and your related object does not + exist, the templatetag will raise a ObjectNotFound exception. It will fail + silently if you set `settings.DEBUG` to `False` and return a (not saved) + instance of the related model. \ No newline at end of file diff --git a/example_project/templates/500.html b/example_project/templates/500.html new file mode 100644 index 0000000..c801615 --- /dev/null +++ b/example_project/templates/500.html @@ -0,0 +1 @@ +500 internal server error \ No newline at end of file diff --git a/example_project/templates/example.html b/example_project/templates/example.html index 0c8f4e8..fbd017e 100644 --- a/example_project/templates/example.html +++ b/example_project/templates/example.html @@ -46,22 +46,17 @@ will fetch the model instance with the primary key you named in slug. Basically this is a {% include %} tag on model level. {% endcomment %} - {% with 1 as admin_user %} - {% gblock admin_user for "auth.user" with "current_user.html" %} - {% endwith %} + {% gblock 1 for "auth.user" with "current_user.html" %} {% comment %} You can store the related object directly in the context using the "into" argument. This is the quickest way to display any model. The "for" and "as" arguments are ignored. {% endcomment %} - {% with 1 as admin_user %} - {% with "the_user_object" as varname %} - {% gblock 1 for "auth.user" into varname %} -

The first user is {{ the_user_object.username }}!

- {% if the_user_object_admin_url %}edit{% endif %} - {% endwith %} - {% endwith %} + {% gblock 1 for "auth.user" into "the_user_object" %} +

The first user is {{ the_user_object.username }}!

+ {% if the_user_object_admin_url %}edit{% endif %} +
{% comment %}