所以我要建立一个网站。用户可以注册,但注册后需要验证电子邮件。我已完成所有设置,昨天运行良好。但是,今天,当我访问该站点并创建一个新用户时,浏览器陷入尝试发送确认电子邮件的麻烦,它说等待127.0.0.1。在屏幕的左下方。但是创建该用户是因为我可以在数据库中看到它们。
settings.py
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'email'
EMAIL_HOST_PASSWORD = 'password'
views.py
# Create your views here.
def register(request):
if request.method == 'POST':
form = UserCreateForm(request.POST)
if form.is_valid():
# Create an inactive user. User will be unable to login until email is verified:
user = form.save(commit=False)
user.is_active = False
user.save()
# Send an email to the user with the token using the email template in the template folder.
current_site = get_current_site(request)
mail_subject = 'Activate your account.'
message = render_to_string('acc_active_email.html', {
'user': request.user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
# The email is being sent using the SMTP configuration in settings.py
to_email = form.cleaned_data.get('email')
email = EmailMessage(mail_subject, message, to=[to_email])
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
else:
form = UserCreateForm()
return render(request, 'register.html', {'form': form})
email template
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uid=uid token=token %}
{% endautoescape %}
因此,如果通过,它将向用户发送一封带有令牌链接的电子邮件,他/她可以单击该令牌并将其重定向回该网站。