Search code examples
djangodjango-viewsdjango-formsemail-verificationdjango-formwizard

Django Forms: Connecting two functions together for Email Verification


I have created a basic Create User form (username, email, password1, password2) called CreateUserForm and i would like to create an email verification process. It is simple enough to do using one function. But i want to add an additional function that asks the user to accept Terms and Conditions before a verification email is sent. In addition the terms and conditions has to be on a separate html template page. I have also created a Terms and Conditions form which is just a simple BoolianField. I am getting an error saying: ''User' object has no attribute 'get'. Do i have to use a request.get somewhere in my code? A steer in the right direction would be helpful. My code is below:

view.py:

def registerPage(request):
    form = CreateUserForm()
    if request.method =='POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)# Save user data but do not commit to database
            return redirect('accept_terms') and instance #accept_terms is the name of the registration/terms_and_conditions.html
    else:
        form = CreateUserForm()
    return render(request,'registration/register.html', {'form': form})

def AcceptTerms(request):
    form = Terms_Conditions()
    if request.method =='POST':
        form = Terms_Conditions(request.POST)
        if form.is_valid():
            userform = registerPage # I want to get the saved User data from the registerPage function 
            send_verification_email(request, userform) #Sends verification email
            return HttpResponse('Thank you, an email will be sent for you to verify.')
    else:
        form = Terms_Conditions()
    return render(request,'registration/terms_and_conditions.html', {'form': form})

Edit:

So i have decided to use Form Wizard instead and i have the following code:

class ContactWizard(SessionWizardView):
    template_name = "test.html"

    def done(self, form_list, **kwargs):
        form_data = process_form_data(form_list)
        context = [form_data[0]['username'],form_data[0]['email'],form_data[0]['password1'],form_data[0]['password2']]

        send_verification_email(self.request, context) # Problem is here
        return HttpResponse('Sent')
    
def process_form_data(form_list):
    form_data = [form.cleaned_data for form in form_list]
    return form_data

However i am getting the error 'list' object has no attribute 'save'. I think it does not like the send_verification_email function but i am unclear how to resolve it.


Solution

  • I have solved it. The issue was i was cleaning the data twice once outside the 'send_verification_email' function and once inside it which was throwing the error.

    def done(self, form_list, **kwargs):
    
        form_data = [form for form in form_list]
        first_form = form_data[0]
        send_verification_email(self.request, first_form)
    
        return HttpResponse('Sent')