Django - URL路由问题(无法导入名称'urls')

时间:2021-01-09 18:06:00

I am following the Django tutorial on https://docs.djangoproject.com/en/1.7/intro/tutorial03/, and am trying to get the index view to show up. I've tried the code specified on the page verbatim but keep on getting errors.

我正在关注https://docs.djangoproject.com/en/1.7/intro/tutorial03/上的Django教程,并试图让索引视图显示出来。我已逐字尝试了页面上指定的代码,但继续收到错误。

polls/urls.py:

民调/ urls.py:

from django.conf.urls import patterns, urls
    from polls import views

    urlpatterns = patterns('', 
    url(r'^$', views.index, name='index'),
)

mysite/urls.py:

mysite的/ urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),

)

and finally, the index method in views.py:

最后,views.py中的索引方法:

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def index(request): 
    return HttpResponse("<h1>Hello world!</h1>");

I'm not sure what I'm doing wrong. I keep getting an error that says "cannot import name 'urls'." any help would be appreciated!

我不确定我做错了什么。我一直收到一条错误,上面写着“无法输入名称'网址'。”任何帮助,将不胜感激!

1 个解决方案

#1


11  

The problem is in your import statement - there is no urls function in django.conf.urls package.

问题出在你的import语句中 - 在django.conf.urls包中没有urls函数。

Replace:

更换:

from django.conf.urls import patterns, urls

with:

有:

from django.conf.urls import patterns, url

#1


11  

The problem is in your import statement - there is no urls function in django.conf.urls package.

问题出在你的import语句中 - 在django.conf.urls包中没有urls函数。

Replace:

更换:

from django.conf.urls import patterns, urls

with:

有:

from django.conf.urls import patterns, url