电子邮件在Django不工作有时 [英] Email in Django not working sometimes
问题描述
有时候可以发送邮件。但有时候错误
Index / at / retest / 8 / accepted
字符串索引超出范围
请求方法:POST
请求URL : http://127.0.0.1:8000/retest/8/accepted
Django版本:1.10.5
异常类型:IndexError
异常值:字符串索引超出范围
异常位置:C:\Users\NajmathUmmer\AppData\Local\Programs\Python\\ \\ Python36\lib\email_header_value_parser.py in get_local_part,第1800行
Python可执行文件:C:\Users\Najmath Ummer\djangogirls\myvenv\ Scripts\python.exe
Python版本:3.6.0
Python路径:
['C:\Users\ Najmath嗯
'C:\Users\Najmath Ummer\djangogirls\myvenv\Scripts\python36.zip',
'C:\Users\\ \\ Najmath Ummer\AppData\Local\Programs\Python\Python36\DLLs',
'C:\Users\Najmath Ummer\AppData\Local\Programs\Python\\ \\ Python36 \lib',
'C:\Users\Najmath Ummer\AppData\Local\Programs\Python\Python36',
'C:\Users\ Najmath Ummer\djangogirls\myvenv',
'C:\Users\Najmath Ummer\djangogirls\myvenv\lib\site-packages']
< blockquote>
这是我的代码:
settings.py
EMAIL_USE_TLS = True
EMAIL_HOST ='smtp.gmail.com'
EMAIL_HOST_USER ='abcd@gmail.com'
EMAIL_HOST_PASSWORD ='**********'
EMAIL_PORT = 587
views.py
from django.core.mail import EmailMessage
pre>
def accepted(request,retest_id):
retest = get_object_or_404(Retest,pk = retest_id)
如果request.method =='POST':
retest.is_principal = True
retest.save(update_fields = ['is_principal'])
email = EmailMessage('RMS','您的通知正在等待',to = [request.user。电子邮件])
email.send()
return render(request,'retest / request.html',{'retest':retest})
解决方案如前所述,需要更多信息...但是这个错误可能与到有关。
当您参考:
email = EmailMessage('RMS','您的通知正在等待',to = [request.user。电子邮件])
;您可能会将您的列表翻倍,因为您收到的错误是典型的。
在引用电子邮件的时候检查代码,您可能会找到解决方案(尝试
to = request.user.email
?)。sometimes the mail can be sent.But sometime an error
IndexError at /retest/8/accepted
string index out of range
Request Method: POST
Request URL: http://127.0.0.1:8000/retest/8/accepted
Django Version: 1.10.5
Exception Type: IndexError
Exception Value: string index out of range
Exception Location: C:\Users\NajmathUmmer\AppData\Local\Programs\Python\Python36\lib\email_header_value_parser.py in get_local_part, line 1800
Python Executable: C:\Users\Najmath Ummer\djangogirls\myvenv\Scripts\python.exe Python Version: 3.6.0
Python Path:
['C:\Users\Najmath Ummer\djangogirls\rms', 'C:\Users\Najmath Ummer\djangogirls\myvenv\Scripts\python36.zip', 'C:\Users\Najmath Ummer\AppData\Local\Programs\Python\Python36\DLLs', 'C:\Users\Najmath Ummer\AppData\Local\Programs\Python\Python36\lib', 'C:\Users\Najmath Ummer\AppData\Local\Programs\Python\Python36', 'C:\Users\Najmath Ummer\djangogirls\myvenv', 'C:\Users\Najmath Ummer\djangogirls\myvenv\lib\site-packages']This is my code:
settings.py
EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = 'abcd@gmail.com' EMAIL_HOST_PASSWORD = '**********' EMAIL_PORT = 587
views.py
from django.core.mail import EmailMessage def accepted(request, retest_id): retest = get_object_or_404(Retest, pk=retest_id) if request.method == 'POST': retest.is_principal = True retest.save(update_fields=['is_principal']) email = EmailMessage('RMS', 'Your Notifications are Pending.', to=[request.user.email]) email.send() return render(request, 'retest/request.html' , {'retest': retest})
解决方案As previously stated, more information is needed...but this error is probably in relation to the "to".
When you reference this at:
email = EmailMessage('RMS', 'Your Notifications are Pending.', to=[request.user.email])
; you are probably doubling your list because the error you received is typical for that.Examine your code when referencing who the email is going to and and you probably will find the solution(try
to=request.user.email
?).这篇关于电子邮件在Django不工作有时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!