ImportError: No module named 'django.core.urlresolvers'

前端 未结 11 1442
孤街浪徒
孤街浪徒 2020-11-27 14:04

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:

相关标签:
11条回答
  • 2020-11-27 14:05

    if you want to import reverse, import it from django.urls

    from django.urls import reverse
    
    0 讨论(0)
  • 2020-11-27 14:06

    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.

    0 讨论(0)
  • 2020-11-27 14:06

    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/drf-extensions.git@master

    - pip install git+https://github.com/django-extensions/django-extensions.git@master

    0 讨论(0)
  • 2020-11-27 14:08

    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.

    0 讨论(0)
  • 2020-11-27 14:12

    For django version greater than 2.0 use:

    from django.urls import reverse
    

    in your models.py file.

    0 讨论(0)
  • 2020-11-27 14:13

    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.

    0 讨论(0)
提交回复
热议问题