Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django TemplateSyntaxError: current transaction is aborted, what does this exception mean? Does postgresql 8.4 work fine with django?

The full text of the error is:

TemplateSyntaxError at /

Caught an exception while rendering: current transaction is aborted, commands ignored until end of transaction block

I've recently reinstalled all the software on my computer. The code used to work no problem before. It also still works no problem on a friend's computer and on a development server.

The only thing I can think of, which may have changed is the postgresql server version (and I'm not actually certain whether I tried running it on 8.4 on my old installation or not - it definitely worked on 8.3). Is anyone able to confirm that Django has problems with postgresql 8.4 and/or has any hints as to why I'm having these errors?

Edit 1

In response to Dominic... This doesn't happen just on one page or with one tag (though there are some pages which seem to be ok). The only thing the tags and variables that cause errors have in common is that they happen to access the database somewhere along the way (though not all tags that access the database cause the error). Furthermore, the same code does not create TemplateSyntaxError on other computers.

If I remove a variable or custom template tag that is making the error then this is the chain of events that happens:

  1. I remove the variable or tag that is supposedely causing the error and refresh the page.
  2. Django picks a different variable or tag and the same error occurs, so I keep removing them.
  3. Once all the variables and tags that cause the error are removed, I stop getting a proper error page. I get a plain white page with the following traceback:
Traceback (most recent call last):

  File "/usr/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 279, in run
    self.result = application(self.environ, self.start_response)

  File "/usr/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 651, in __call__
    return self.application(environ, start_response)

  File "/usr/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 245, in __call__
    response = middleware_method(request, response)

  File "/usr/lib/python2.6/site-packages/debug_toolbar/middleware.py", line 90, in process_response
    response.content = replace_insensitive(smart_unicode(response.content), u'', smart_unicode(self.debug_toolbars[request].render_toolbar() + u''))

  File "/usr/lib/python2.6/site-packages/debug_toolbar/toolbar/loader.py", line 72, in render_toolbar
    'BASE_URL': self.request.META.get('SCRIPT_NAME', ''),

  File "/usr/lib/python2.6/site-packages/django/template/loader.py", line 108, in render_to_string
    return t.render(context_instance)

  File "/usr/lib/python2.6/site-packages/django/test/utils.py", line 29, in instrumented_test_render
    return self.nodelist.render(context)

  File "/usr/lib/python2.6/site-packages/django/template/__init__.py", line 779, in render
    bits.append(self.render_node(node, context))

  File "/usr/lib/python2.6/site-packages/django/template/debug.py", line 71, in render_node
    result = node.render(context)

  File "/usr/lib/python2.6/site-packages/django/template/defaulttags.py", line 155, in render
    nodelist.append(node.render(context))

  File "/usr/lib/python2.6/site-packages/django/template/defaulttags.py", line 243, in render
    return self.nodelist_true.render(context)

  File "/usr/lib/python2.6/site-packages/django/template/__init__.py", line 779, in render
    bits.append(self.render_node(node, context))

  File "/usr/lib/python2.6/site-packages/django/template/debug.py", line 81, in render_node
    raise wrapped

TemplateSyntaxError: Caught an exception while rendering: current transaction is aborted, commands ignored until end of transaction block


Original Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/django/template/debug.py", line 71, in render_node
    result = node.render(context)
  File "/usr/lib/python2.6/site-packages/django/template/debug.py", line 87, in render
    output = force_unicode(self.filter_expression.resolve(context))
  File "/usr/lib/python2.6/site-packages/django/template/__init__.py", line 546, in resolve
    obj = self.var.resolve(context)
  File "/usr/lib/python2.6/site-packages/django/template/__init__.py", line 687, in resolve
    value = self._resolve_lookup(context)
  File "/usr/lib/python2.6/site-packages/django/template/__init__.py", line 722, in _resolve_lookup
    current = current()
  File "/usr/lib/python2.6/site-packages/debug_toolbar/panels/template.py", line 64, in content
    pformat(k(self.request))) for k in get_standard_processors()
  File "/usr/lib/python2.6/site-packages/django/core/context_processors.py", line 27, in auth
    'messages': user.get_and_delete_messages(),
  File "/usr/lib/python2.6/site-packages/django/contrib/auth/models.py", line 263, in get_and_delete_messages
    for m in self.message_set.all():
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 106, in _result_iter
    self._fill_cache()
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 692, in _fill_cache
    self._result_cache.append(self._iter.next())
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 238, in iterator
    for row in self.query.results_iter():
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", line 287, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/python2.6/site-packages/debug_toolbar/panels/sql.py", line 91, in execute
    return self.cursor.execute(sql, params)
InternalError: current transaction is aborted, commands ignored until end of transaction block
like image 334
Monika Sulik Avatar asked Dec 08 '09 14:12

Monika Sulik


2 Answers

That exception means that there was an error in some SQL that is getting executed. Since Django runs all the SQL inside of a database transaction, all the SQL that is being executed after the error gets ignored. So:

BEGIN;
SELECT * FROM table;
SELECT missing_column FROM table WHERE id = 1; -- generates an error because the column is missing
SELECT * FROM another_table; -- this statement and all following statements get ignored until the next COMMIT;
COMMIT;

To figure out the problem, find your log file for PostgreSQL and run tail -f /path/to/postgresql_error.log. Then refresh the page. You should see the error come up in the log file.

like image 177
sheats Avatar answered Oct 06 '22 01:10

sheats


If you're moving from a different DB, note that there might be some considerations to bear in mind with the way that Django with Postgres uses transactions. Have a read here:

http://docs.djangoproject.com/en/dev/topics/db/transactions/?from=olddocs#handling-exceptions-within-postgresql-transactions

The quick answer is usually to turn on database level autocommit by adding:

'OPTIONS': {'autocommit': True,}

to your database settings. But it's worth having a read up on it and understanding the coo.

Rolo.

like image 29
Rolo Avatar answered Oct 06 '22 00:10

Rolo