Skip to content

Instantly share code, notes, and snippets.

@dcramer
Created March 13, 2011 12:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dcramer/868061 to your computer and use it in GitHub Desktop.
Save dcramer/868061 to your computer and use it in GitHub Desktop.
from threading import local
_blah = local()
class StopThatShit(Exception):
pass
def patch():
from django.db.backends import util
from django import template
class StopQueryingCursorWrapper(util.CursorWrapper):
def __init__(self, cursor, connection):
if getattr(_blah, 'rendering', False):
raise StopThatShit('Stop executing queries in your templates!')
super(StopQueryingCursorWrapper, self).__init__(cursor, connection)
util.CursorWrapper = StopQueryingCursorWrapper
class StopQueryingTemplate(template.Template):
def render(self, context):
_blah.rendering = True
try:
super(StopQueryingTemplate, self).__init__(context)
finally:
_blah.rendering = False
template.Template = StopQueryingTemplate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment