Skip to content

Commit

Permalink
The templatetag will raise an exception if a related object is not fo…
Browse files Browse the repository at this point in the history
…und using the "into" argument. It will fail silently if settings.DEBUG is False.
  • Loading branch information
bartTC committed Sep 8, 2009
1 parent 9a9d0c6 commit 5f8f00d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 2 additions & 0 deletions django_generic_flatblocks/templatetags/generic_flatblocks.py
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion docs/ref/usage.rst
Expand Up @@ -97,4 +97,10 @@ directly without creating a template::

would be rendered as::
The first user is johndoe! (<a href="/admin/auth/user/1/">edit</a>)
The first user is johndoe! (<a href="/admin/auth/user/1/">edit</a>)

.. 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.
1 change: 1 addition & 0 deletions example_project/templates/500.html
@@ -0,0 +1 @@
500 internal server error
15 changes: 5 additions & 10 deletions example_project/templates/example.html
Expand Up @@ -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 %}
<p>The first user is {{ the_user_object.username }}!</p>
{% if the_user_object_admin_url %}<a href="{{ the_user_object_admin_url }}">edit</a>{% endif %}
{% endwith %}
{% endwith %}
{% gblock 1 for "auth.user" into "the_user_object" %}
<p>The first user is {{ the_user_object.username }}!</p>
{% if the_user_object_admin_url %}<a href="{{ the_user_object_admin_url }}">edit</a>{% endif %}

<hr/>

{% comment %}
Expand Down

0 comments on commit 5f8f00d

Please sign in to comment.