ACCOUNT_EMAIL_VERIFICATION='强制'不工作

2024-04-24 09:49:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想有一个网站,验证它的用户发送验证电子邮件。为了寻找一个好的教程,我来到this 这对我帮助很大,但我仍然有问题。在

我不知道为什么没有电子邮件发送到我注册的示例电子邮件,也没有写入PyCharm终端(正如教程中所说)。在

你能帮帮我吗?我在哪方面犯了错误?在

我的代码:

在#设置.py在

SITE_ID = 1

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug': DEBUG,
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.i18n',
                "django.template.context_processors.media",

            ],
        },
    },
]
# Custom allauth settings
# Use email as the primary identifier
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_REQUIRED = True
# Make email verification mandatory to avoid junk email accounts
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
# Eliminate need to provide username, as it's a very old practice
ACCOUNT_USERNAME_REQUIRED = False

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin and to ensure compatibility with other packages
    'django.contrib.auth.backends.ModelBackend',
    # 'allauth' specific authentication methods
    'allauth.account.auth_backends.AuthenticationBackend',
)

在#网址.py在

^{pr2}$

在#视图.py在

def register(request):
    registered = False
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        registration_form = RegistrationForm(data=request.POST)

        if user_form.is_valid() and registration_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = registration_form.save(commit=False)
            profile.email = user_form.cleaned_data['email']
            profile.user = user

            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']
            profile.save()
            registered = True

            new_user = authenticate(username=user_form.cleaned_data['username'],
                                    password=user_form.cleaned_data['password'], )
            login(request, new_user)

        else:
            print user_form.errors, registration_form.errors

    else:
        user_form = UserForm()
        registration_form = RegistrationForm()

    return render(request, 'register.html',
                  {'user_form': user_form, 'profile_form': registration_form, 'registered': registered})

还有我的表单.py公司名称:

class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'username', 'email', 'password')

    password = forms.CharField(required=True, widget=forms.PasswordInput())
    confirm_password = forms.CharField(required=True, widget=forms.PasswordInput())

    def clean_confirm_password(self):
        password = self.cleaned_data.get('password', '')
        confirm_password = self.cleaned_data.get("confirm_password", '')
        if password != confirm_password:
            print("error")
            raise forms.ValidationError("Passwords do not match!")
        return confirm_password



class RegistrationForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        exclude = ['user', 'email']

Tags: djangoformdataemailrequestcontextusernametemplate