简体   繁体   中英

Django - URL routing issues (cannot import name 'urls')

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.

polls/urls.py:

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

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

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:

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!

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

Replace:

from django.conf.urls import patterns, urls

with:

from django.conf.urls import patterns, url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM