我正在尝试制作一个可以通过电子邮件重置用户密码的应用程序。我正在使用Django by Example书籍第3章,并且也参考了这个网站的指导:https://simpleisbetterthancomplex.com/tutorial/2016/09/19/how-to-create-password-reset-view.html
Settings.py文件中包含以下行:
在“POST”输出之后,我期望看到对“.console.EmailBackend”设置的邮件转储的响应,但从未得到过任何东西。
导致模拟发送电子邮件失败的原因是什么?
环境:
Windows 7、django 2.0,在CMD控制台下,使用pipenv shell。
编辑1:
在另一台机器上也出现了同样的问题。
编辑2:
更改为“.smtp”不会改变任何内容,也没有错误。
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
urls.py
。 - JeffPurls.py
不匹配。 - Burhan Khalid