I have a function friend_exists
like this:
def friend_exists(request, pid):
result = False
try开发者_Go百科:
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?
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
.
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)
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.
精彩评论