I am following the official django tutorial https://docs.djangoproject.com/en/1.10/intro/tutorial04/
我正在关注官方的django教程https://docs.djangoproject.com/en/1.10/intro/tutorial04/
I am using Django 1.9.8 and I am now getting the following error in /polls/views.py
:
我正在使用Django 1.9.8,现在我在/polls/views.py中收到以下错误:
File "C:\Users\hk\Documents\mysite\polls\views.py", line 4, in <module>
from django.urls import reverse
ImportError: No module named urls
polls/views.py
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from .models import Choice, Question
# ...
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns=[
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
mysite/mysite/urls.py
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
3 个解决方案
#1
8
That tutorial requires Django 1.10 while you seem to be using an older version. In this case, you should import reverse
as follows:
该教程需要Django 1.10,而您似乎使用的是旧版本。在这种情况下,您应该反向导入,如下所示:
from django.core.urlresolvers import reverse
To upgrade your Django version, use the following command (in your command line, not in the Python shell):
要升级Django版本,请使用以下命令(在命令行中,而不是在Python shell中):
pip install --upgrade django
#2
1
Latest tutorial (https://docs.djangoproject.com/en/2.0/intro/tutorial01/) uses Django 2.0, which supports Python 3.4 and later
最新教程(https://docs.djangoproject.com/en/2.0/intro/tutorial01/)使用Django 2.0,它支持Python 3.4及更高版本
Since I ran into this same issue I had to upgrade by python as well as django
由于我遇到了同样的问题,我不得不通过python和django进行升级
sudo apt-get -y install python3-pip
pip3 install --upgrade django
to see your django version run following command -
看看你的django版本运行以下命令 -
python3 -c "import django; print(django.get_version())"
for me it says 2.0.1
对我来说它说2.0.1
To check python version run -
检查python版本运行 -
python3 --version
For me it is Python 3.5.2
对我来说这是Python 3.5.2
#3
0
At times, if one has multiple copies of django running on one's machine like I had, if the current project requires a different version of sjango, it is easier to simply go to preferences -> project interpreter -> add a virtual env -> install latest copy of django
. Now pycharm will interpret the project correctly.
有时,如果有一个django的副本像我一样在一台机器上运行,如果当前项目需要不同版本的sjango,则更容易简单地转到首选项 - >项目解释器 - >添加虚拟环境 - >安装django的最新副本。现在pycharm将正确解释项目。
#1
8
That tutorial requires Django 1.10 while you seem to be using an older version. In this case, you should import reverse
as follows:
该教程需要Django 1.10,而您似乎使用的是旧版本。在这种情况下,您应该反向导入,如下所示:
from django.core.urlresolvers import reverse
To upgrade your Django version, use the following command (in your command line, not in the Python shell):
要升级Django版本,请使用以下命令(在命令行中,而不是在Python shell中):
pip install --upgrade django
#2
1
Latest tutorial (https://docs.djangoproject.com/en/2.0/intro/tutorial01/) uses Django 2.0, which supports Python 3.4 and later
最新教程(https://docs.djangoproject.com/en/2.0/intro/tutorial01/)使用Django 2.0,它支持Python 3.4及更高版本
Since I ran into this same issue I had to upgrade by python as well as django
由于我遇到了同样的问题,我不得不通过python和django进行升级
sudo apt-get -y install python3-pip
pip3 install --upgrade django
to see your django version run following command -
看看你的django版本运行以下命令 -
python3 -c "import django; print(django.get_version())"
for me it says 2.0.1
对我来说它说2.0.1
To check python version run -
检查python版本运行 -
python3 --version
For me it is Python 3.5.2
对我来说这是Python 3.5.2
#3
0
At times, if one has multiple copies of django running on one's machine like I had, if the current project requires a different version of sjango, it is easier to simply go to preferences -> project interpreter -> add a virtual env -> install latest copy of django
. Now pycharm will interpret the project correctly.
有时,如果有一个django的副本像我一样在一台机器上运行,如果当前项目需要不同版本的sjango,则更容易简单地转到首选项 - >项目解释器 - >添加虚拟环境 - >安装django的最新副本。现在pycharm将正确解释项目。