我试图了解基于类的视图和django。该项目为notes_project
,其中我创建了一个应用notes
。以下是这两个urls.py
和views.py
应用的notes
:
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)),
)
备注/ urls.py
from django.conf.urls import include, patterns, url
from .views import IndexView
urlpatterns = patterns(r'^$/', IndexView.as_view())
备注/ 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")
但是,每当我访问网址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']
答案 0 :(得分:0)
patterns
的第一个参数是一个字符串,作为其余模式的前缀。每个模式也需要是它自己的元组。你已经在主urls.py中正确地完成了这一点,但在笔记一中错过了它。它应该是:
urlpatterns = patterns('',
(r'^$', IndexView.as_view()),
)