View只需要2个参数(给定1个)

时间:2014-12-05 11:46:00

标签: python django

在主urls.py

url(r'^request/(Req_.*)/someoperation/',include(someoperation.urls))

someoperation.urls

url(r'^query$', queryPage),

queryPage看起来像:

def queryPage(request, request_id):
    #somestuff
    return HttpResponse('OK')

开启的网址是:

myhost:myport/request/Req_ABCXYZ/someoperation/query

但是我收到了这个错误:

ec/2014 05:41:18] ERROR [django.request:215] Internal Server Error: /request/Req_ABCXYZ/someoperation/query
Traceback (most recent call last):
  File "/opt/xyz/build/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
TypeError: queryPage() takes exactly 2 arguments (1 given)

可能发生什么事?我确信Req_.*匹配Req_ABCXYZ;所以它应该传递给视图queryPage

有任何线索吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

url(r'^request/(?P<request_id>Req_.*)/someoperation/',include(someoperation.urls))

答案 1 :(得分:0)

就像错误消息所说的那样,当它预期两个变量时,你只会将一个变量传递给该函数。假设您的函数需要request_id,您必须修改urls.py,以便从URL捕获request_id并将其传递给视图。

这个简单的例子可以让你知道如何去做:

urls.py

urlpatterns = patterns('',
    url(r'^(?P<slug>[\w\-]+)/$', 'base.views.index'),
)

views.py

def index(request, slug=None):
    if slug is not None:
        return HttpResponse(slug)
    else:
        return HttpResponse("No slug provided")

基本上是将你的正则表达式包装在(?INSERT_REGEX_HERE)标签中的情况,所以在你的情况下它将是:

(?P<request_id>Req_.*)