title: How can i avoid a user from registering an already used email in django?
body: <p>I was testing my registration view and i noticed that if i try to register using an alreaxy existing Username, i'll get an error; if i try to register using an already existing email, the app will let me do so.</p>
  
  <p>Obviously, i don't want someone to register multiple accounts with the same email on my site. I'm fairly new to Django, and since i noticed that the form checked if an username already exists, i thought it would do the same with the email field.</p>
  
  <p>I don't really know how to go from that, should i work on my view or on the form? And how can i make it loop through my DB and find if an e-mail had already been registered? I thought <code>email = form.cleaned_data.get('email')</code> would do the trick, but it didn't.
  Any help is appreciated.</p>
  
  <p>Here is my view:</p>
  
  <pre><code>def register(request):
      if request.method == "POST":
          form = NewUserForm(request.POST)
          if form.is_valid():
              user = form.save()
              username = form.cleaned_data.get('username')
              email = form.cleaned_data.get('email')
              messages.success(request, f"New Account Created: {username}")
              login(request, user)
              messages.info(request, f"You are now logged in as {username}")
              return redirect("main:homepage")
          else:
              for msg in form.error_messages:
                  messages.error(request, f"{msg}: {form.error_messages[msg]}")
  
  
      form = NewUserForm
      return render(request,
                    "main/register.html",
                    context={"form":form})
  </code></pre>
  
  <p>And here is the form:</p>
  
  <pre><code>class NewUserForm(UserCreationForm):
      email = forms.EmailField(required=True)
  
  
      class Meta:
          model = User
          fields = ("username", "email", "password1", "password2")
  
  
      def save(self, commit=True):
          user = super(NewUserForm, self).save(commit=False)
          user.email = self.cleaned_data['email']
          if commit:
              user.save()
          return user
  </code></pre>