2015-06-03 293 views
0

我有一个奇怪的问题.....我摆弄Django的电子邮件后端...测试控制台和smtp后端.....电子邮件发送两次!我找不到为什么它happenning ....Django电子邮件发送两次

这里是调用电子邮件发送操作的观点:

from django.http import HttpResponse 
from django.core.mail import send_mail, EmailMessage 

def index(request): 
    if request.method in ('GET'): 
     print request.method 
     mail = EmailMessage(subject='Subject Here', body='Here be the msg!', from_email='[email protected]', to=['[email protected]']) 
     mail.send() 
     #send_mail(subject='Subject Here', message='Here be the msg!', from_email='[email protected]', recipient_list=['[email protected]']) 

    return HttpResponse('Mail Sent') 

可以看出,我用send()方法,也都EmailMessage类send_mail()函数.....但两者表现相同.....并且电子邮件被发送两次!

任何帮助?

+3

'print'语句是否也运行两次? – rnevius

+0

多一个,在'to'参数'to = ['[email protected]']'电子邮件ID不能重复。 –

+1

只是一个提示......你可能想使用POST而不是GET来避免滥用。 – JOSEFtw

回答

0

我有同样的问题,并花了数小时寻找解决方案,我认为我找到了解决方案from this link。我认为我的网络浏览器可能是这个问题的根源。 我有一个AJAX调用Django的视图,然后一个window.redirect

request = $.ajax({ 
    url: "{% url 'add_to_cart' %}", 
    type: "post", 
    data: { 
     ajax: 'yes', 
     ids: JSON.stringify(IDs), 
     xxxx: $("#xxxx").val(), 
     csrfmiddlewaretoken: getCookie('csrftoken') 
    } 
}).done(function (response) { 
    if (response == "OK") { 
     $('#cart_name').val(''); 
     window.location.href = '/'; 
    } 
}); 

我只是删除window.location.href = '/'; 这防止布劳尔发送Ajax调用两次。我没有太多时间来调查为什么一个简单的window.location.href = '/';进行第二次不想要的呼叫。