使用gmail和Django注册不会重置密码电子邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用gmail和Django注册不会重置密码电子邮件相关的知识,希望对你有一定的参考价值。
我正在尝试使用Gmail发送重置密码电子邮件。下面的重置功能工作,并在填写有效的电子邮件地址并提交发送后重定向到“成功”页面,但没有发送邮件。所有注册模板都存储在同一个目录中;的myapp /注册。
在设置中
import smtplib
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587 #465
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_HOST_USER = 'youremail@gmail.com'
#EMAIL_SUBJECT_PREFIX = 'something'
EMAIL_USE_TLS = True
在网址中
url(r'^password/change/$',
auth_views.password_change,
{'post_change_redirect': reverse_lazy('auth_password_change_done')},
name='password_change'),
url(r'^password/change/done/$',
auth_views.password_change_done,
name='password_change_done'),
url(r'^password/reset/$',
auth_views.password_reset,
{'post_reset_redirect': reverse_lazy('auth_password_reset_done')},
name='password_reset'),
url(r'^password/reset/done/$',
auth_views.password_reset_done,
name='password_reset_done'),
url(r'^password/reset/complete/$',
auth_views.password_reset_complete,
name='password_reset_complete'),
url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z_-]+)/(?P<token>.+)/$',
auth_views.password_reset_confirm,
{'post_reset_redirect': reverse_lazy('auth_password_reset_complete')},
name='password_reset_confirm'),
#and now add the registration urls
url(r'', include('registration.backends.default.urls')),
在视图中
def password_reset(request, is_admin_site=False,
template_name='registration/password_reset_form.html',
email_template_name='registration/password_reset_email.html',
subject_template_name='registration/password_reset_subject.txt',
password_reset_form=PasswordResetForm,
token_generator=default_token_generator,
post_reset_redirect=None,
from_email=None,
current_app=None,
extra_context=None,
html_email_template_name=None):
if post_reset_redirect is None:
post_reset_redirect = reverse('password_reset_done')
else:
post_reset_redirect = resolve_url(post_reset_redirect)
if request.method == "POST":
form = password_reset_form(request.POST)
if form.is_valid():
opts = {
'use_https': request.is_secure(),
'token_generator': token_generator,
'from_email': from_email,
'email_template_name': email_template_name,
'subject_template_name': subject_template_name,
'request': request,
'html_email_template_name': html_email_template_name,
}
if is_admin_site:
opts = dict(opts, domain_override=request.get_host())
form.save(**opts)
return HttpResponseRedirect(post_reset_redirect)
else:
form = password_reset_form()
context = {
'form': form,
'title': _('Password reset'),
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
def password_reset_done(request,
template_name='registration/password_reset_done.html',
current_app=None, extra_context=None):
context = {
'title': _('Password reset successful'),
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
@sensitive_post_parameters()
@never_cache
def password_reset_confirm(request, uidb64=None, token=None,
template_name='registration/password_reset_confirm.html',
token_generator=default_token_generator,
set_password_form=SetPasswordForm,
post_reset_redirect=None,
current_app=None, extra_context=None):
"""
View that checks the hash in a password reset link and presents a
form for entering a new password.
"""
UserModel = get_user_model()
assert uidb64 is not None and token is not None # checked by URLconf
if post_reset_redirect is None:
post_reset_redirect = reverse('password_reset_complete')
else:
post_reset_redirect = resolve_url(post_reset_redirect)
try:
uid = urlsafe_base64_decode(uidb64)
user = UserModel._default_manager.get(pk=uid)
except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
user = None
if user is not None and token_generator.check_token(user, token):
validlink = True
title = _('Enter new password')
if request.method == 'POST':
form = set_password_form(user, request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(post_reset_redirect)
else:
form = set_password_form(user)
else:
validlink = False
form = None
title = _('Password reset unsuccessful')
context = {
'form': form,
'title': title,
'validlink': validlink,
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
def password_reset_complete(request,
template_name='registration/password_reset_complete.html',
current_app=None, extra_context=None):
context = {
'login_url': resolve_url(settings.LOGIN_URL),
'title': _('Password reset complete'),
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
def password_change(request,
template_name='registration/password_change_form.html',
post_change_redirect=None,
password_change_form=PasswordChangeForm,
current_app=None, extra_context=None):
if post_change_redirect is None:
post_change_redirect = reverse('password_change_done')
else:
post_change_redirect = resolve_url(post_change_redirect)
if request.method == "POST":
form = password_change_form(user=request.user, data=request.POST)
if form.is_valid():
form.save()
# Updating the password logs out all other sessions for the user
# except the current one if
# django.contrib.auth.middleware.SessionAuthenticationMiddleware
# is enabled.
update_session_auth_hash(request, form.user)
return HttpResponseRedirect(post_change_redirect)
else:
form = password_change_form(user=request.user)
context = {
'form': form,
'title': _('Password change'),
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
@login_required
def password_change_done(request,
template_name='registration/password_change_done.html',
current_app=None, extra_context=None):
context = {
'title': _('Password change successful'),
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
UPDATE
我的电子邮件现在发送到gmail但gmail提供以下错误消息;
Delivery to the following recipient failed permanently:
to_email_address@gmail.com
Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the server for the recipient domain gmail.com by gmail-smtp-in.l.google.com. [2a00:1450:4010:c04::1b].
The error that the other server returned was:
550-5.1.1 The email account that you tried to reach does not exist. Please try
550-5.1.1 double-checking the recipient's email address for typos or
550-5.1.1 unnecessary spaces. Learn more at
550 5.1.1 http://support.google.com/mail/bin/answer.py?answer=6596 ok4si3174885lbb.110 - gsmtp
----- Original message -----
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=from:content-type:mime-version:content-transfer-encoding:subject:to
:date:message-id;
bh=XIT/LAmN67IRi9HrLcISdd0LCPh/Bo8BtcnOZ3a1r2M=;
b=jIlBIOhiXVxumR9TmTigQTJL23bh5NiaGHVZr7zOdIbVaQ/o3Ud3F7S7xlYdgrdF7X
f8kZwMY59Q405rfxv5dmkDm5cAu7XTHJANpbhdMgk0zDySh9ohxJSPDNZ53fMDMubPZN
Y2HAYFGrB51nyld/wSfBJ/tOgxZ25kA//g/1wLhfCZOiU5zOVlMhl/T38W7bIRXpgSuy
MwPWeUWJ7HfLOaCbuXNcizVvUxzieq5aKrIw5I16TmNfkp40oCR3oBnBR1hx8gvPim4x
6IR+GhAwo5Zj9XNmFhnIp/EjxU4DV9OsgUelAqfyPRQ80M4RWE/qaWXqxJs2HGJ6+2gI
bc/w==
X-Received: by 10.112.61.136 with SMTP id p8mr23610717lbr.107.1429696044965;
Wed, 22 Apr 2015 02:47:24 -0700 (PDT
)
我认为这可能与重置视图绑定到Django管理员视图有关,所以我尝试通过使用我自己的base.html扩展它来更改注册模板,就像在这个博客http://garmoncheg.blogspot.se/2012/07/django-resetting-passwords-with.html中所做的那样但它没有'与...合作。我还在视图中添加了以下change_password函数。
在urls.py中
url(r'^password/change/$',
auth_views.password_change,
{'post_change_redirect': reverse_lazy('auth_password_change_done')},
name='my_password_change'),
url(r'^password/change/done/$',
auth_views.password_change_done,
name='password_change_done'),
url(r'^password/reset/$',
auth_views.password_reset,
{'post_reset_redirect': reverse_lazy('auth_password_reset_done')},
name='password_reset'),
url(r'^password/reset/done/$',
auth_views.password_reset_done,
name='password_reset_done'),
url(r'^password/reset/complete/$',
auth_views.password_reset_complete,
name='password_reset_complete'),
url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z_-]+)/(?P<token>.+)/$',
auth_views.password_reset_confirm,
{'post_reset_redirect': reverse_lazy('auth_password_reset_complete')},
name='password_reset_confirm'),
#and now add the registration urls
url(r'', include('registration.backends.default.urls')),
在视图中
from django.contrib.auth.views import password_change
def my_password_change(request):
return password_change(template_name='password_change_form.html', extra_context={'my_var1': my_var1})
def password_change(request,
template_name='registration/password_change_form.html',
post_change_redirect=None,
password_change_form=PasswordChangeForm,
current_app=None, extra_context=None):
if post_change_redirect is None:
post_change_redirect = reverse('django.contrib.auth.views.password_change_done')
if request.method == "POST":
form = password_change_form(user=request.user, data=request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(post_change_redirect)
else:
form = password_change_form(user=request.user)
context = {
'form': form,
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
遗憾的是,我没有在你编写的视图函数中看到任何邮件发送代码(即send_mail(...)
),这可能是在password_reset
或password_reset_form
中。您需要添加它以发送电子邮件(例如),如下所示:
from django.core.mail import send_mail
send_mail('subject', 'email_body', 'from_email_address@gmail.com' , ['to_email_address@gmail.com'], fail_silently=False)
您是否在运行代码后尝试登录Gmail帐户?
您可能必须允许您的应用使用Gmail发送电子邮件。
https://support.google.com/accounts/answer/6010255?hl=en
这是我通过gmail smtp在django中发送邮件的工作配置:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youruser@gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
以上是关于使用gmail和Django注册不会重置密码电子邮件的主要内容,如果未能解决你的问题,请参考以下文章
电子邮件验证和密码重置 - django rest 框架和 angularjs
Django Social Auth - 我无法重置通过Facebook签名的用户的密码