Can I print out the context from within a template?

2008-04-18 Thread Salim Fadhley

I'm trying to debug the context that is passed into the template. When
I render the template I use a command like:

return HttpResponse( render_to_string('mtmreport/index.html'),
myContext )

Is it possible to write something in the template that simply prints
out the myContext object?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Can I print out the context from within a template?

2008-04-18 Thread [EMAIL PROTECTED]

You could write a simple template tag that did this.

On Apr 18, 12:26 pm, Salim Fadhley <[EMAIL PROTECTED]> wrote:
> I'm trying to debug the context that is passed into the template. When
> I render the template I use a command like:
>
> return HttpResponse( render_to_string('mtmreport/index.html'),
> myContext )
>
> Is it possible to write something in the template that simply prints
> out the myContext object?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Can I print out the context from within a template?

2008-04-18 Thread yml

Hello,
Here it is an extract form the documentation :
"""
If you use a variable that doesn’t exist, the template system will
insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is
set to '' (the empty string) by default
"""
It looks like it does what you want.
--yml

On Apr 18, 8:34 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> You could write a simple template tag that did this.
>
> On Apr 18, 12:26 pm, Salim Fadhley <[EMAIL PROTECTED]> wrote:
>
> > I'm trying to debug the context that is passed into the template. When
> > I render the template I use a command like:
>
> > return HttpResponse( render_to_string('mtmreport/index.html'),
> > myContext )
>
> > Is it possible to write something in the template that simply prints
> > out the myContext object?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Can I print out the context from within a template?

2008-04-18 Thread Salim Fadhley

yml,

If I want to disable this behavior, is it possible? When a template
tries to reference an invalid object (e.g. a context variable which
has not been defined) I'd like to throw an exception. I do not want to
invent a value.

My application prints financial reports - it's important to make a
distinction between missing data and intentionally blank spaces.

:-)

On Apr 18, 7:46 pm, yml <[EMAIL PROTECTED]> wrote:
> Hello,
> Here it is an extract form the documentation :
> """
> If you use a variable that doesn’t exist, the template system will
> insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is
> set to '' (the empty string) by default
> """
> It looks like it does what you want.
> --yml
>
> On Apr 18, 8:34 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
> wrote:
>
> > You could write a simple template tag that did this.
>
> > On Apr 18, 12:26 pm, Salim Fadhley <[EMAIL PROTECTED]> wrote:
>
> > > I'm trying to debug the context that is passed into the template. When
> > > I render the template I use a command like:
>
> > > return HttpResponse( render_to_string('mtmreport/index.html'),
> > > myContext )
>
> > > Is it possible to write something in the template that simply prints
> > > out the myContext object?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Can I print out the context from within a template?

2008-04-18 Thread Norman Harman

Salim Fadhley wrote:
> yml,
> 
> If I want to disable this behavior, is it possible? When a template
> tries to reference an invalid object (e.g. a context variable which
> has not been defined) I'd like to throw an exception. I do not want to
> invent a value.

In my test_settings.py file I put

TEMPLATE_STRING_IF_INVALID = "test_oops_test"

I've subclassed Django's UnitTest.assertTemplateUsed to fail (with 
appropriate message) if it finds that text in the rendered html. And, of 
course, there are unittests that "exercise" all my views and their 
variations.

Not exactly what you are asking for but I find it better to fail 
unittests than throw exception at runtime.

Alternatively or in addition you could set your production settings.py 
to something like,

TEMPLATE_STRING_IF_INVALID = "this is a bug please email 
[EMAIL PROTECTED]"

-- 
Norman J. Harman Jr.
Technology Solutions Group, Austin American-Statesman
___
Get out and about this spring with the Statesman! In print and online,
the Statesman has the area's Best Bets and recreation events.
Pick up your copy today or go to statesman.com 24/7.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Can I print out the context from within a template?

2008-04-18 Thread Malcolm Tredinnick


On Fri, 2008-04-18 at 11:49 -0700, Salim Fadhley wrote:
> yml,
> 
> If I want to disable this behavior, is it possible? When a template
> tries to reference an invalid object (e.g. a context variable which
> has not been defined) I'd like to throw an exception. I do not want to
> invent a value.

No, you can't. The current behaviour is a design feature of the
templates, which help make writing templates that handle polymorphic
objects a lot easier.

Note that no value is "invented" when you access an attribute that
doesn't exist. Instead, None is returned. It's very consistent and
displays quite well (as nothing).

In the past, we have suggested that people could write a middleware to
look for the TEMPLATE_STRING_IF_INVALID string in the output. However,
you have to be a little careful there, since things like the admin
interface take advantage of the template behaviour, so you will break
your admin display if you use TEMPLATE_STRING_IF_INVALID (that setting
is really only for testing -- so adjusting your test suite to check for
it, as suggested elsewhere in this thread, is also a good plan).

Regards,
Malcolm

-- 
Depression is merely anger without enthusiasm. 
http://www.pointy-stick.com/blog/


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---