[Answer]-How to combine 2 forms for use on the same page?

1👍

There is valid debate about whether to keep the User and UserProfile separated. If you want to use separate classes:

@csrf_protect
def register(request, extra_context=None):
    if request.method == 'POST':

        user_form = AddUserForm(data=request.POST, files=request.FILES)
        user_profile_form = AddUserProfileForm(data=request.POST, files=request.FILES)

        if user_form.is_valid() and user_profile_form.is_valid():
            new_user = user_form.save(request.get_host())
            new_user_profile = user_profile_form.save(request.get_host(), new_user)
            return HttpResponseRedirect(reverse('registration_complete'))
    else:
        user_form = AddUserForm()
        user_profile_form = AddUserProfileForm()

    context = {}
    return render_to_response('registration_form.html',
                            {   'user_form': user_form,
                                'user_profile_form': user_profile_form },
                            context_instance=context)  
👤GAEfan

Leave a comment