views:

57

answers:

3

I have a function friend_exists like this:

def friend_exists(request, pid):
    result = False
    try:
        user = Friend.objects.get(pid=pid)
    except Friend.DoesNotExist:
        pass  
    if user:
        result = True

    return result

I'm calling it from my other function like this:

exists = friend_exists(form.cleaned_data['pid'])

where pid = u'12345678'. Why I'm getting:

Exception Type: TypeError at /user/register/
Exception Value: friend_exists() takes exactly 2 arguments (1 given)

Any ideas?

+2  A: 

It takes two arguments and you are only giving it one, the value of form.cleaned_data['pid']. If that value is actually a tuple/list of the two arguments, you want to expand it with the asterisk like:

exists = friend_exists(*form.cleaned_data['pid'])

A cleaner approach in that case might then be:

request, pid = form.cleaned_data['pid']
exists = friend_exists(request, pid)
mrooney
+5  A: 

Why do you think it should only take one? You've clearly got two arguments in the function definition:

def friend_exists(request, pid):

Right there it says it expects request and pid.

Daniel Roseman
If that's the actual source for the `friend_exists` function, it doesn't look like the `request` argument is needed. Maybe that one argument is some accidental holdover after refactoring (or something)?
Will McCutchen
I just thought request is automatically populated
muntu
+1  A: 

This looks like django, so the way to properly call your function would be friend_exists(request, form.cleaned_data['pid']. When a view function is called, the request is automatically populated, so it may seem like that should happen for every call in a django app, but as you are calling the function manually, you will have to manually pass it the request object.

Blue Peppers