N
N
Nicknameme2021-03-27 13:44:49
Django
Nicknameme, 2021-03-27 13:44:49

Problem with email uniqueness check?

Hi all. I have a user registration form
forms.py:

from django import forms
from django.contrib.auth.models import User

class UserRegisrationForm(forms.ModelForm):
    password = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Repeat Password', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('username', 'first_name', 'email')

    def clean_password2(self):
        cd = self.cleaned_data
        if cd['password'] != cd['password2']:
            raise forms.ValidationError('Passwords don\'t match.')
        return cd['password2']

    def clean_email(self):
        cd = self.cleaned_data
        if User.objects.get(email=cd['email']):
            raise forms.ValidationError('User with such mail exists. Use another.')
        return cd['email']

The password check works great. But mail check works partially. If you enter the mail of an existing user during registration, then everything works correctly, that is, it issues 'User with such mail exists. Use another.'. And if you enter another mail that is not in the database, it gives an error when saving the user.

Internal Server Error: /account/register/
Traceback (most recent call last):
File "C:\Users\User\Desktop\dj\movie\venv\lib\site-packages\django\core\handlers\exception.py" , line 47, in inner
response = get_response(request)
File "C:\Users\User\Desktop\dj\movie\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\Desktop\dj\dj in examples\chapter 4 social\bookmarks\account\views.py", line 37, in register
if user_form.is_valid():
File "C:\Users\ User\Desktop\dj\movie\venv\lib\site-packages\django\forms\forms.py", line 177, in is_valid
return self.is_bound and not self.errors
File "C:\Users\User\Desktop\ dj\movie\venv\lib\site-packages\django\forms\forms.py", line 172, in errors
self.full_clean()
File "C:\Users\User\Desktop\dj\movie\venv\lib\ site-packages\django\forms\forms.py", line 374, in full_clean
self._clean_fields()
File "C:\Users\User\Desktop\dj\movie\venv\lib\site-packages\django\forms\ forms.py", line 395, in _clean_fields
value = getattr(self, 'clean_%s' % name)()
File "C:\Users\User\Desktop\dj\dj in examples\chapter 4 social\bookmarks\account\forms.py", line 26, in clean_email
if User.objects.get(email=cd['email' ]):
File "C:\Users\User\Desktop\dj\movie\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset() , name)(*args, **kwargs)
File "C:\Users\User\Desktop\dj\movie\venv\lib\site-packages\django\db\models\query.py", line 429, in get
raise self.model.DoesNotExist(
django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.

What's the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2021-03-27
@Nicknameme

User.objects.filter(email=email).exists()
The bottom line is that if a user with such an email is not found, an exception is generated.
It happens here:
User.objects.get(email=cd['email'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question