Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name reverse_lazy

Tags:

python

django

I'm very new to python and trying to run a piece of Django code on my system, but I'm running into this problem.

$ python manage.py runserver
Running in development mode.
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    import settings
  File "/Users/Kinnovate/Desktop/fsdjango/platformsite/settings.py", line 321, in <module>
    from django.core.urlresolvers import reverse_lazy
ImportError: cannot import name reverse_lazy

I'm using python 2.7. How do I fix this?

like image 725
henghonglee Avatar asked Dec 02 '22 23:12

henghonglee


2 Answers

reverse_lazy is newer than any released version of Django. Are you sure you have a trunk version of Django?

like image 80
Ned Batchelder Avatar answered Dec 04 '22 13:12

Ned Batchelder


if you're stuck with 1.3 for a while you can use something along these lines:

try:
    from django.core.urlresolvers import reverse_lazy
except ImportError:
    from django.core.urlresolvers import reverse
    from django.utils.functional import lazy
    reverse_lazy = lambda *args, **kwargs: lazy(reverse, str)(*args, **kwargs)

Update: reverse_lazy handling variable args

like image 36
tnajdek Avatar answered Dec 04 '22 12:12

tnajdek