I am working on Django project where I need to create a form for inputs. I tried to import reverse from django.core.urlresolvers. I got an error:
line 2, in from django.core.urlresolvers import reverse ImportError: No module named 'django.core.urlresolvers'
I am using Python 3.5.2, Django 2.0 and MySQL.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Django 2.0 removes the django.core.urlresolvers module, which was moved to django.urls in version 1.10. You should change any import to use django.urls instead, like this:
from django.urls import reverse
Note that Django 2.0 removes some features that previously were in django.core.urlresolvers, so you might have to make some more changes before your code works. See the features deprecated in 1.9 for details on those additional changes.
Method 2
if you want to import reverse, import it from django.urls
from django.urls import reverse
Method 3
You need replace all occurrences of:
from django.core.urlresolvers import reverse
to:
from django.urls import reverse
NOTE: The same apply to reverse_lazy
in Pycharm Cmd+Shift+R for starting replacment in Path.
Method 4
use from django.urls import reverse instead of from django.core.urlresolvers import reverse
Method 5
For those who might be trying to create a Travis Build, the default path from which Django is installed from the requirements.txt file points to a repo whose django_extensions module has not been updated. The only workaround, for now, is to install from the master branch using pip. That is where the patch is made. But for now, we’ll have to wait.
You can try this in the meantime, it might help
- pip install git+https://github.com/chibisov/[email protected]
- pip install git+https://github.com/django-extensions/[email protected]
Method 6
For django version greater than 2.0 use:
from django.urls import reverse
in your models.py file.
Method 7
urlresolver has been removed in the higher version of Django – Please upgrade your django installation. I fixed it using the following command.
pip install django==2.0 --upgrade
Method 8
If your builds on TravisCI are failing for this particular reason, you can resolve the issue by updating the Django Extensions in your requirements.txt
pip install --upgrade django-extensions
This will update the extensions to use Django 2+ modules.
Method 9
To solve this either you down-grade the Django to any version lesser than 2.0. install
pipDjango==1.11.29.
Method 10
Upgrading Django 1.9 (Python 2.7) to Django 3.2 (Python 3.9)
This could be solved in a one line bash replacement:
grep -ril "from django.core.urlresolvers" your_source_code_folder | xargs sed -i '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a192a0c180507">[email protected]</a> <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfbbb5beb1b8b0f1bcb0adbaf1aaadb3adbaacb0b3a9baadac9fb9adb0b2">[email protected]</a> djan<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a4d45045f5846596a4d">[email protected]</a>'
Method 11
In my case the problem was that I had outdated django-stronghold installed (0.2.9). And even though in the code I had:
from django.urls import reverse
I still encountered the error. After I upgraded the version to django-stronghold==0.4.0 the problem disappeard.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
