title: Duplicate key oiolates unique constraint during signup
body: <blockquote>
    <p>duplicate key value violates unique constraint "auth_user_username_key"
    DETAIL:  Key (username)=() already exists.</p>
  </blockquote>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>class Profile(User):
      user = models.OneToOneField(User)
      mobile = models.CharField(max_length=100, null=True, blank=True)
      country_code = models.CharField(max_length=100, null=True, blank=True)
      shipping_street_1 = models.CharField(max_length=100, null=True, blank=True)
      shipping_street_2 = models.CharField(max_length=100, null=True, blank=True)
      shipping_city = models.CharField(max_length=100, null=True, blank=True)
      shipping_state = models.CharField(max_length=100, null=True, blank=True)
      shipping_country = models.CharField(max_length=100, null=True, blank=True)
      shipping_zip = models.CharField(max_length=100, null=True, blank=True)
      shipping_tel = models.CharField(max_length=100, null=True, blank=True)
      billing_street_1 = models.CharField(max_length=100, null=True, blank=True)
      billing_street_2 = models.CharField(max_length=100, null=True, blank=True)
      billing_city = models.CharField(max_length=100, null=True, blank=True)
      billing_state = models.CharField(max_length=100, null=True, blank=True)
      billing_country = models.CharField(max_length=100, null=True, blank=True)
      billing_zip = models.CharField(max_length=100, null=True, blank=True)
      billing_tel = models.CharField(max_length=100, null=True, blank=True)
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>class SignUpView(FormView):
      model = models.User
      template_name = "registration/signup.html"
      form_class = forms.SignupForm
      success_url = "/"
  
      @method_decorator(csrf_exempt)
      def dispatch(self, request, *args, **kwargs):
          return super(SignUpView, self).dispatch(request, *args, **kwargs)
  
      def form_valid(self, form):
          e = models.User.objects.filter(email=self.request.POST.get("signup_email")).first()
          if e is None:
              user_obj, created = \
                  u = models.User.objects.get_or_create(
                  email=self.request.POST.get("signup_email"),
                  username=self.request.POST.get("signup_email"),
              )
              user_obj.set_password(self.request.POST.get("signup_password"))
              user_obj.save()
              login(self.request, user_obj)
  
              models.Profile.objects.create(
                  user=user_obj,
                  mobile=self.request.POST.get("signup_mobile"),
                  country_code=self.request.POST.get("signup_country_code")
              )
          return super(SignUpView, self).form_valid(form)
  </code></pre>
  
  <p>It works the first time but database entry is blank. Second time it gives this error.</p>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>class SignupForm(forms.Form):
      signup_email = forms.EmailField(required=True)
      signup_password = forms.CharField(widget=forms.PasswordInput, required=True)
      signup_confirm_password = forms.CharField(widget=forms.PasswordInput, required=True)
      signup_first_name = forms.CharField(required=True)
      signup_last_name = forms.CharField(required=True)
      signup_country_code = forms.CharField(required=True)
      signup_mobile = forms.CharField()
  
      def __init__(self, *args, **kwargs):
          super(SignupForm, self).__init__(*args, **kwargs)
          self.fields['signup_email'].widget.attrs.update({'placeholder': "Email"})
          self.fields['signup_password'].widget.attrs.update({'placeholder': 'Password'})
          self.fields['signup_confirm_password'].widget.attrs.update({'placeholder': 'Confirm Password'})
          self.fields['signup_first_name'].widget.attrs.update({'placeholder': "First Name"})
          self.fields['signup_last_name'].widget.attrs.update({'placeholder': "Last Name"})
          self.fields['signup_country_code'].widget.attrs.update({'placeholder': "Country Code"})
          self.fields['signup_mobile'].widget.attrs.update({'placeholder': "Mobile Number"})
  
      def clean_signup_confirm_password(self):
          if self.cleaned_data.get('signup_password') and self.cleaned_data.get('signup_password'):
              if self.cleaned_data.get('signup_password') != self.cleaned_data.get('signup_confirm_password'):
                  raise forms.ValidationError("The two password fields must match.")
          return self.cleaned_data['signup_confirm_password']
  </code></pre>