def registration(request):
    registered = False

    if request.method == 'POST':
        userForm = UserForm(data = request.POST)
        profileForm = UserProfileInfoForm(data = request.POST)

        if userForm.is_valid() and profileForm.is_valid():
            user = userForm.save()
            user.set_password(user.password)
            user.save()

            profile = profileForm.save(commit=False)
            profile.user = user

            if 'profilePic' in request.FILES:
                profile.profilePic = request.FILES['profilePic']

            profile.save()

            registered = True

        else:
            print(userForm.errors,profileForm.errors)
    else:
        userForm = UserForm
        profileForm = UserProfileInfoForm

    return render(request,'basicApp/registration.html',{'userForm':userForm,'profileForm':profileForm,'registered':registered})
Example #2
0
def register(request):
    registered = False

    if request.method == 'POST':

        try:
            user_form = UserForm(data=request.POST)
            profile_form = UserProfileInfoForm(data=request.POST)

            if user_form.is_valid() and profile_form.is_valid():
                user = user_form.save()
                user.set_password(user.password)

                profile = profile_form.save(commit=False)
                profile.user = user

                if 'profile_pic' in request.FILES:
                    profile.profile_pic = request.FILES['profile_pic']

                profile.save()
                registered = True

            else:
                print(user_form.errors, profile_form.errors)
        except ObjectDoesNotExist:
            print('FFF')
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()
    return render(
        request, 'basicApp/register.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })
Example #3
0
def register(request):
    registered = False
    if request.method == "POST":
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()  #getting form data to database
            user.set_password(user.password)  # hashing done
            user.save()  #saving the password

            profile = profile_form.save(
                commit=False
            )  #not saving directly to D.B. to check presemce of profile pic
            profile.user = user  #setting the one to one relationship with the User class

            if 'profile_pic' in request.FILES:  #use request.FILES to access any kind of file submitted
                profile.profile_pic = request.FILES['profile_pic']

            profile.save()

            registered = True
        else:
            print(user_form.errors, profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()

    return render(
        request, 'basicApp/registration.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })
def register(request):

    registered = False

    if request.method == 'POST':

        # Get info from "both" forms
        # It appears as one form to the user on the .html page
        user_form = UserForm(data=request.POST)
        profile_form = UserInfoForm(data=request.POST)

        # Check to see both forms are valid
        if user_form.is_valid() and profile_form.is_valid():

            # Save User Form to Database
            user = user_form.save()

            # Hash the password
            user.set_password(user.password)

            # Update with Hashed password
            user.save()

            # Now we deal with the extra info!

            # Can't commit yet because we still need to manipulate
            profile = profile_form.save(commit=False)

            # Set One to One relationship between
            # UserForm and UserProfileInfoForm
            profile.user = user

            # Check if they provided a profile picture
            if 'profile_pic' in request.FILES:
                print('found it')
                # If yes, then grab it from the POST form reply
                profile.profile_pic = request.FILES['profile_pic']

            # Now save model
            profile.save()

            # Registration Successful!
            registered = True

        else:
            # One of the forms was invalid if this else gets called.
            print(user_form.errors,profile_form.errors)

    else:
        # Was not an HTTP post so we just render the forms as blank.
        user_form = UserForm()
        profile_form = UserInfoForm()

    # This is the render and context dictionary to feed
    # back to the registration.html file page.
    return render(request,'basicApp/register.html',
                          {'user_form':user_form,
                           'profile_form':profile_form,
                           'registered':registered})