Answering myself, in case this is useful to anyone in the future...

I eventually discovered the problem lay in the method which checked
submitted comments for spam (a method along these lines:
http://sciyoshi.com/blog/2008/aug/27/using-akismet-djangos-new-comments-framework/
). It expected the request object to have META['HTTP_REFERER'] and
META['HTTP_USER_AGENT']. When the comment was submitted from a unit
test, these weren't present and this was causing things to fall over.

I've no idea why that generated what looked like a template error, but
checking for the presence of these, and using empty strings if they're
not present, has got things working again.


On Thu, Nov 11, 2010 at 5:30 PM, Phil Gyford <gyf...@gmail.com> wrote:
> Hi,
>
> I have a unit test that tests that a comment can be posted on an entry
> in a Django blog. Posting a comment myself in the browser works fine,
> but the test always fails with this error:
>
> "TemplateSyntaxError: Caught VariableDoesNotExist while rendering:
> Failed lookup for key [request] in u'[{}]'"
>
> Which suggests the use of the 'request' variable in a template isn't
> working. I guess it's a ContextProcessor problem, but I'm stuck as to
> why it fails in the test. Here's the test:
>
> ----
> from django.test.client import Client
> from django.test import TestCase
> import datetime, time
> from weblog.models import Entry
> from django.contrib.comments.forms import CommentSecurityForm
>
> class WeblogBaseTestCase(TestCase):
>    fixtures = ['../../aggregator/fixtures/test_data.json', ]
>
> class CommentTestCase(WeblogBaseTestCase):
>
>    def post_comment(self, entry_id=1):
>        form = CommentSecurityForm( Entry.live.get(pk=entry_id) )
>        timestamp = int(time.time())
>        security_hash = form.initial_security_hash(timestamp)
>        c = Client()
>        response = c.post('/cmnt/post/', {
>                                'name': 'Bobby',
>                                'email': 'bo...@example.com',
>                                'url': '',
>                                'comment': 'Hello',
>                                'content_type': 'weblog.entry',
>                                'timestamp': timestamp,
>                                'object_pk': entry_id,
>                                'security_hash': security_hash,
>                            },follow=True)
>
>        self.failUnlessEqual(response.status_code, 200)
> ----
>
> I'm stumped as to why this error is generated in the test, but not on
> the site, and I'm not sure how to poke around to see where it's going
> wrong. Any thoughts?
>
> Thanks.
>
> --
> http://www.gyford.com/
>



-- 
http://www.gyford.com/

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

Reply via email to