【发布时间】:2021-02-08 06:43:53
【问题描述】:
我正在尝试发送电子邮件验证码以确认帐户。
现在,问题是我想先给自己发一封电子邮件,然后再给我的另一封电子邮件。我已经关闭了我的防病毒软件,所以这应该不是问题。除此之外,我无法弄清楚我做错了什么,因为它没有在 gmail 帐户上发送电子邮件。请指出我做错了什么。我什至应用了this 线程提到的所有修复。
views.py
from django.core.mail import send_mail
from django.shortcuts import render,redirect
from django.contrib import messages,auth
from django.contrib.auth.models import User # this table already exists in django we import it
from django.contrib.auth import authenticate, login
from django.conf import settings
from django.core.mail import EmailMessage
def register(request):
if request.method=='POST':
fname = request.POST['fname']
lname=request.POST['lname'] #request.post[id of the input field]
email = request.POST['email']
password = request.POST['pass']
password2 = request.POST['confirmpass']
agree=request.POST.get('agree')
if fname == '':
messages.warning(request, 'Please enter First name!')
return redirect('register')
if lname == '':
messages.warning(request, 'Please enter Last name!')
return redirect('register')
if email == '':
messages.warning(request, 'Please enter Email!')
return redirect('register')
if password == '':
messages.warning(request, 'Please enter Password!')
return redirect('register')
if password2 == '':
messages.warning(request, 'Please enter Confirm Password!')
return redirect('register')
if ('agree' not in request.POST):
messages.warning(request, 'Please agree to our terms and conditions!')
return redirect('register')
if password==password2:
if User.objects.filter(username=email):
messages.error(request,"Email Already Exists")
return redirect('register')
else:
user=User.objects.create_user(username=email,first_name=fname,last_name=lname,password=password) #these are postgres first_name,last_name
user.save()
messages.success(request,"Successfully Registered")
subject = 'Site Contact Form'
contact_message = "%s : %s via %s "%(fname, lname, email)
from_email = settings.EMAIL_HOST_USER
to_email = [from_email, 'myotheremail@gmail.com']
send_mail(subject, contact_message, from_email, to_email, fail_silently=False)
return redirect('login')
else:
messages.error(request,"Passwords don't match")
return redirect('register')
else:
return render(request, 'signupdiv.html')
settings.py
EMAIL_HOSTS = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = '*****'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('login', views.login, name='login'),
path('register', views.register, name='register'),
path('logout', views.logout, name='logout'),
]
当我注册时,数据成功存储到数据库中(如预期的那样)。但它在代码的 view.py 中给出了 send_email(params) 错误。以下是错误:
ConnectionRefusedError at /account/register
[WinError 10061] No connection could be made because the target machine actively refused it
Request Method: POST
Request URL: http://127.0.0.1:8000/account/register
Django Version: 3.1.2
Exception Type: ConnectionRefusedError
Exception Value:
[WinError 10061] No connection could be made because the target machine actively refused it
Exception Location: C:\Users\User\AppData\Local\Programs\Python\Python37\lib\socket.py, line 716, in create_connection
Python Executable: C:\Users\User\Desktop\pfa\venv\Scripts\python.exe
Python Version: 3.7.5
Python Path:
['C:\\Users\\User\\Desktop\\pfa',
'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37',
'C:\\Users\\User\\Desktop\\pfa\\venv',
'C:\\Users\\User\\Desktop\\pfa\\venv\\lib\\site-packages']
Server time: Sun, 25 Oct 2020 23:17:08 +0000
【问题讨论】:
标签: python django authentication smtp email-validation