【发布时间】:2013-06-26 08:26:37
【问题描述】:
我刚刚开始使用 Django,并且正在使用 djangobook.com。我尝试了动态 URL 示例,但它给了我一个 TypeError。你能看出什么问题吗?
views.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)
urls.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>
【问题讨论】:
-
链接写成 localhost/name 等,因为 SO 不允许我输入 127.0 等
标签: python django dynamic-url