我刚刚开始使用 Django,并且正在使用 djangobook.com。我尝试了动态 URL 示例,但它给了我一个 TypeError。你能看出有什么问题吗?
视图.py
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
import datetime
def nameOffset(request, offset):
print "in nameOffset"
t = get_template('base.html')
html = t.render(Context({'name':offset}))
return HttpResponse(html)
网址.py
from django.conf.urls import patterns, include, url
from MemberInterface.views import getName, nameOffset
urlpatterns = patterns('',
(r'^name/$', getName ),
(r'^name/plus/\d+/$', nameOffset ),
)
/localhost/name/ 一切正常
但是当我去 /localhost/name/plus/1/ 时,我得到
TypeError at /name/plus/1/
nameOffset() takes exactly 2 arguments (1 given)
Request Method: GET Request URL: /localhost/name/plus/1/
Django Version: 1.5.1 Exception Type: TypeError Exception Value:
nameOffset() takes exactly 2 arguments (1 given)
“2 个参数,一个给定”是什么意思.. 参数是 request 和 offset... 并且 request 内部不是通过 get 传递的吗?
编辑:
这是base.html
<html>
<title> Test Project </title>
<body>
Hello {{ name }}
</body>
</html>