user1265125
user1265125

Reputation: 2656

View takes exactly 2 arguments (1 given)

In the main urls.py:

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

In someoperation.urls:

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

queryPage looks like:

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

The URL getting opened is:

myhost:myport/request/Req_ABCXYZ/someoperation/query

But I get this error:

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)

What could be going on? I'm sure Req_.* matches Req_ABCXYZ; so it should be getting passed on to the view queryPage.

Any clue?

Upvotes: 0

Views: 1270

Answers (2)

david_hughes
david_hughes

Reputation: 762

Like the error message says, you're only passing one variable to the function, when it's expecting two. Assuming that request_id is required for your function to work you'll have to modify your urls.py so that it captures the request_id from the URL and passes it to the view.

This simple example should give you an idea how to go about it:

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")

It's basically a case of wrapping your regex in (?INSERT_REGEX_HERE) tag, so in your case it would be:

(?P<request_id>Req_.*)

Upvotes: 0

madzohan
madzohan

Reputation: 11808

Try this:

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

Upvotes: 1

Related Questions