django - 'function' 对象没有属性 'resolve'

django - 'function' object has no attribute 'resolve'

我正在尝试了解基于 class 的视图和一般的 Django。该项目是 notes_project,我在其中创建了一个应用程序 notes。以下是这两个应用程序的 urls.pynotes 应用程序的 views.py

notes_project/urls.py

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


urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'notes_project.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^notes/', include('notes.urls')),
    url(r'^grappelli/', include('grappelli.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

notes/urls.py

from django.conf.urls import include, patterns, url
from .views import IndexView


urlpatterns = patterns(r'^$/', IndexView.as_view())

notes/views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import View


class IndexView(View):

    def get(request):
        return HttpResponse("Welcome to notes index")

但是,每当我访问 URL http://127.0.0.1:8000/notes/ 时,我总是收到以下错误:

Request Method: GET
Request URL:    http://127.0.0.1:8000/notes/
Django Version: 1.7.4
Exception Type: AttributeError
Exception Value:    
'function' object has no attribute 'resolve'
Exception Location: /path/notes/venv/lib/python3.4/site-packages/django/core/urlresolvers.py in resolve, line 345
Python Executable:  /path/notes/venv/bin/python
Python Version: 3.4.2
Python Path:    
['/path/notes/notes_project',
 '/path/notes/venv/lib/python3.4',
 '/path/notes/venv/lib/python3.4/plat-x86_64-linux-gnu',
 '/path/notes/venv/lib/python3.4/lib-dynload',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/path/notes/venv/lib/python3.4/site-packages']

patterns 的第一个参数是一个字符串,作为其余模式的前缀。此外,每个模式都需要是自己的元组。您在主 urls.py 中正确地完成了该操作,但在注释一中遗漏了它。应该是:

urlpatterns = patterns('',
    (r'^$', IndexView.as_view()),
)