title: Django: Verify email before creating a user
body: <p>I have a Django project. I would like to verify email before creating a user account. What's the best way to do so? I decided to create an independent model first but I'm confused now.</p>
  <p>I decided not to use allauth model because it's too complex. Instead, I'm generating a random number and sending it over email. Once the activation code is confirmed, a user should type a password. After obtaining a verified email and password, I need to create a User model. Email will be used as a username.</p>
  <p><strong>EmailVerifyModel</strong></p>
  <pre><code>class EmailVerifyModel(models.Model):
      email = models.EmailField(help_text=_('A valid email address please'))
      isVerified = models.BooleanField(default=False)
      activation_code = models.IntegerField(blank=True)
  </code></pre>
  <p><strong>EmailVerifyForm</strong></p>
  <pre><code>class EmailVerifyForm(forms.ModelForm):
      email = forms.EmailField(widget = forms.TextInput(attrs={
          &quot;type&quot;: &quot;email&quot;,
          &quot;name&quot;: &quot;email&quot;,
          &quot;id&quot;: &quot;email&quot;,
          &quot;placeholder&quot;: _(&quot;Email&quot;),
          &quot;class&quot;: &quot;form-control form-control-md&quot;,
          &quot;required&quot;: &quot;&quot;
      }), label = &quot;&quot;)
  
      class Meta:
          model = EmailVerifyModel
          fields = ('email', 'activation_code')
          widgets = {
              'activation_code': NumberInput(attrs={'class': 'form-control','placeholder': 'Verification code'}),
          }
  </code></pre>
  <p><strong>View</strong></p>
  <pre><code>def signup_view(request):
      form = EmailVerifyForm(request.POST or None)
      email_verified = False
      
      if form.is_valid():
          
          new_email = form.save(commit=False)
          pin = randint(1000,9999)
          new_email.activation_code = pin
          new_email.save()
  
          cd = form.cleaned_data
  
          if not cd['activation_code']:
              current_site = get_current_site(request)
              subject = _('Verify Your Email')
          
              email_body = render_to_string('email/verify-email.html', {
                          'domain': current_site.domain,
                          'token': pin,
                      })
              msg = EmailMessage(subject, email_body, 'no-reply@test.com', ['test@test.com'])
              msg.send()
              email_verified = True
  
          elif cd['activation_code']:
              email = EmailVerifyModel.objects.get(email = form.instance.email)
              print('user with email', email)
  
  
      return render(
          request,
          'signup.html',
          {
              'form': form,
              'email_verified': email_verified, 
          }
      )
  </code></pre>