gpt4 book ai didi

python - "ImportError: No module named urls"遵循 Django 教程

转载 作者:太空狗 更新时间:2023-10-30 00:43:34 25 4
gpt4 key购买 nike

我正在关注官方 django 教程 https://docs.djangoproject.com/en/1.10/intro/tutorial04/

我正在使用 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')),
]

最佳答案

该教程需要 Django 1.10,而您似乎使用的是旧版本。在这种情况下,您应该按如下方式导入 reverse:

from django.core.urlresolvers import reverse

要升级您的 Django 版本,请使用以下命令(在您的命令行中,不是在 Python shell 中):

pip install --upgrade django

关于python - "ImportError: No module named urls"遵循 Django 教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com