I want to implement an invitation feature where user will fill in email address and their email will be saved to database. If they again fill in email address and request for invitation, they should be redirected to another form(ReferForm) so they can refer other.
Here is my code
class InviteForm(forms.Form):
email = forms.EmailField(label=_("E-mail"), required=True)
def save(self, email):
print('email', email)
print ('############')
if (Invitation.objects.get(email=email)):
return True
invitation = Invitation.objects.create(email=email)
return invitation
class RequestInvitation(FormView):
template_name = 'home.html'
form_class = InviteForm
def form_valid(self, form):
email = form.cleaned_data.get('email')
invite_instance = form.save(email)
if invite_instance == True:
return HttpResponseRedirect('/refer-form')
invite_instance.invited_by_email_address = self.request.user
invite_instance.custom_invite_code = get_custom_invite_code()
invite_instance.save()
messages.success(self.request, '{0} has been invited'.format(email))
return HttpResponseRedirect('/')
def form_invalid(self, form):
# messages.error(self.request, '{0}'.format(form.errors))
return self.render_to_response(self.get_context_data(form=form))
I get an error inside form save function where i have queried for email existence. However i get an error
Invitation matching query does not exist.