此代码以前对我有用,但现在无法正常工作。一切运行正常,但在完成注册过程后未向用户发送电子邮件。此代码有解决方案吗?
这是 views.py
def activate(req, uidb64, token):
try:
uid = urlsafe_base64_decode(uidb64).decode()
user = User.objects.get(id=uid)
except(TypeError, ValueError):
user = None
if user and activation_token.check_token(user, token):
user.is_active = True
user.save()
messages.info(req, 'Your Account activated. Now Login')
return redirect("shop:users_signin")
else:
messages.error(req, "Activation link is Invalid.")
def users_signup(req):
if req.method == "POST":
form = UserSignupForm(req.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
site = get_current_site(req)
mail_subject = "Confirmation message"
message = render_to_string('shop/activate_mail.html', {
"user": user,
'domain': site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': activation_token.make_token(user)
})
to_email = form.cleaned_data.get('email')
to_list = [to_email]
from_email = settings.EMAIL_HOST_USER
send_mail(mail_subject, message, from_email, to_list, fail_silently=True)
messages.success(req,"Thanks for your registration. A confirmation link has been sent to your email")
else:
form = UserSignupForm()
return render(req,'shop/users_signup.html',{'form':form})
这是我的网址
urls.py
path('signup/user/',views.users_signup,name='users_signup'),
path('activate/<uidb64>/<token>/', views.activate, name='activate'),
这是我的设置。py
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER ='my email'
EMAIL_HOST_PASSWORD = 'my pass'
EMAIL_POST = '587'
这是我的tokens.py
tokens.py
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
class tokenGenerate(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.id)+six.text_type(timestamp)+six.text_type(user.is_active)
)
activation_token=tokenGenerate()
这是我的activate_mail.html
activate_mail.html
Hi {{user.username}} Thanks for your registration in our college finder application.
Please click on the link below to activate your account.
http://{{domain}}{% url 'shop:activate' uidb64=uid token=token %}