Я хочу подтвердить регистрацию нового пользователя, отправив токен подтверждения на электронную почту пользователя. код не показывает ошибок в консоли, но также не отправляет электронное письмо на адрес электронной почты пользователя. Я использую django 3.0 и python 3.8.
код выполняет эту часть, когда я регистрирую нового пользователя, это сообщение появляется, но я не получаю электронную почту "Open your email to activate account."
помогите мне исправить это:
views.py
from .forms import CreateUserForm
from django.views import View
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from .tokens import activation_token
from django.contrib.auth.models import User
from django.contrib import messages
class RegisterPageView(View):
def get(self, request, *args, **kwargs):
form = CreateUserForm()
template_name = "register/register.html"
return render(request, template_name, {"form": form})
def post(self, request, *args, **kwargs):
form = CreateUserForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
message_subject = "Activate your account"
domain_url = get_current_site(request)
user_email = form.cleaned_data["email"]
message = render_to_string(
"Backend/accounts/activation_message.html",
{
"domain": domain_url.domain,
"user": user,
"uid": urlsafe_base64_encode(force_bytes(user.id)),
"token": activation_token.make_token(user),
},
)
email = EmailMessage(message_subject, message, to=[user_email])
email.send()
activation_msg = "Open your email to activate account."
return render(
request, "Backend/accounts/activate_email.html", {"activation_msg": activation_msg}
)
template_name = 'register/register.html'
return render(request, template_name, {"form": form})
urls.py
from django.urls import path
from .views import *
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', index, name="index"),
path("activate/<uidb64>/<token>", activate, name="activate"),
path("register/", RegisterPageView.as_view(), name="register"),
]
forms.py
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
class CreateUserForm(UserCreationForm):
class Meta:
model = User
fields = ("username", "email", "password1", "password2","first_name","last_name")
def clean_email(self):
data = self.cleaned_data["email"]
try:
user_email = User.objects.get(email=data)
except User.DoesNotExist:
pass
else:
raise forms.ValidationError("Email already exist")
settings .py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'mymail'
EMAIL_HOST_PASSWORD = 'mypassword'