Django startup importing causes reverse to happen

Posted by nicknack on Stack Overflow See other posts from Stack Overflow or by nicknack
Published on 2009-09-26T00:54:40Z Indexed on 2010/04/22 16:03 UTC
Read the original article Hit count: 189

Filed under:
|
|

This might be an isolated problem, but figured I'd ask in case someone has thoughts on a graceful approach to address it.

Here's the setup:

--------
views.py
--------
from django.http import HttpResponse
import shortcuts

def mood_dispatcher(request):
  mood = magic_function_to_guess_my_mood(request)
  return HttpResponse('Please go to %s' % shortcuts.MOODS.get(mood, somedefault))


------------
shortcuts.py
------------
MOODS = # expensive load that causes a reverse to happen

The issue is that shortcuts.py causes an exception to be thrown when a reverse is attempted before django is done building the urls. However, views.py doesn't yet need to import shortcuts.py (used only when mood_dispatcher is actually called). Obvious initial solutions are: 1) Import shortcuts inline (just not very nice stylistically) 2) Make shortcuts.py build MOODS lazily (just more work)

What I ideally would like is to be able to say, at the top of views.py, "import shortcuts except when loading urls"

© Stack Overflow or respective owner

Related posts about django

Related posts about django-urls