我正在尝试通过电子邮件重置用户密码。我正在使用Django by Example第3章,并从本网站获取指导:https://simpleisbetterthancomplex.com/tutorial/2016/09/19/how-to-create-password-reset-view.html
Settings.py包含以下行:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
以及所需的电子邮件设置。
我进入password_reset
视图,输入虚拟邮件,点击发送,最后将我带到password_reset_done
视图。以下是控制台输出:
C:\PythonProjects\by-example-bookmarks>python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
May 01, 2018 - 21:13:39
Django version 2.0.4, using settings 'bookmarks.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[01/May/2018 21:13:51] "GET /account/login/ HTTP/1.1" 200 1090
[01/May/2018 21:13:57] "GET /account/password_reset/ HTTP/1.1" 200 830
[01/May/2018 21:14:04] "POST /account/password_reset/ HTTP/1.1" 302 0
[01/May/2018 21:14:05] "GET /account/password_reset/done/ HTTP/1.1" 200 602
在"POST"
输出后,我希望看到电子邮件的转储以响应.console.EmailBackend
设置,但是什么都没有。
无法模拟发送电子邮件的原因是什么?
环境:
Windows 7,django 2.0,在CMD控制台上,在pipenv shell下
修改1 同一问题在另一台机器上。
编辑2
更改为.smtp
不会改变任何内容,也不会发生错误。
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
编辑3 我尝试过其他人的工作,例如:https://github.com/ruddra/django-reset-password/tree/master
仍然没有运气。
编辑4 又一个“别人的工作”。仍然没有运气。 https://github.com/wsvincent/django-auth-tutorial
它有用吗?
编辑5:views.py
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth import authenticate, login
from .forms import LoginForm
from django.contrib.auth.decorators import login_required
def user_login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
user = authenticate( username=cd['username'],
password=cd['password'])
if user is not None:
if user.is_active:
login(request, user)
return HttpResponse('Authenticated '\
'successfully')
else:
return HttpResponse('Disabled account')
else:
return HttpResponse('Invalid login')
else:
form = LoginForm()
return render(request, 'account/login.html', {'form': form})
@login_required
def dashboard(request):
return render(request,
'account/dashboard.html',
{'section': 'dashboard'})
urls.py
from django.conf.urls import url, include
from . import views
import django.contrib.auth.views
# i think the following does not serve any purpose anymore
from django.contrib.auth import views as auth_views
urlpatterns = [
# previous login view
# url(r'^login/$', views.user_login, name='login'),
# login / logout urls
url(r'^login/$',
django.contrib.auth.views.login,
name='login'),
url(r'^logout/$',
django.contrib.auth.views.logout,
name='logout'),
url(r'^logout-then-login/$',
django.contrib.auth.views.logout_then_login,
name='logout_then_login'),
url(r'^$', views.dashboard, name='dashboard'),
url(r'^password-change/$',
django.contrib.auth.views.password_change,
name='password_change'),
url(r'^password-change/done/$',
django.contrib.auth.views.password_change_done,
name='password_change_done'),
# restore password urls
url('^', include('django.contrib.auth.urls')),
]
编辑6 我的github回购就在这里,万一有人好奇:https://github.com/jeffplata/by-example-account