[Django]-Django must be a "User" instance

3👍

Please try this

def add(request, user_id,template_name='friends/add.html'):
     if request.method == 'POST':
         user = get_object_or_404(User, pk=user_id)

         form = InviteFriendForm(request.user, request.POST)
         if form.is_valid():
             form.save()
             return HttpResponseRedirect('/')

     else:
         form = InviteFriendForm()

     context = { 'form':form, }

     return render_to_response(template_name,
context,
context_instance=RequestContext(request))text_instance=RequestContext(request))

request.user is the sender. and the sender is call in the form. you dont need to declare the from.

👤Ralph

1👍

In your call:

form = InviteFriendForm(UserForm, from_user, user)

First, the UserForm argument is wrong — the first argument is expected to be the post data (request.POST).

Second, your InviteFriendForm class doesn’t define a constructor (__init__ method). So the arguments aren’t passed on to the parent class UserForm. Either define an __init__ method or use keyword arguments, e.g. user=from_user.

👤ars

Leave a comment