2015-06-03 185 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 = '/';進行第二次不想要的呼叫。