2010-04-16 88 views
3

我無法找到此代碼中的錯誤。我試過隔離這個問題,但它工作當我將相關的代碼複製到一個單獨的文件。問題必須與周圍的代碼,但我不明白它甚至是相關的。這裏的一切爲什麼Django發送錯誤的電子郵件模板?

問題是與「激活您的PROJECT帳戶」電子郵件。它向我發送一封電子郵件,它是這樣的:

--===============1413769924== 
Content-Type: text/plain; charset="utf-8" 
MIME-Version: 1.0 
Content-Transfer-Encoding: quoted-printable 

text here 

--===============1413769924== 
Content-Type: text/html; charset="utf-8" 
MIME-Version: 1.0 
Content-Transfer-Encoding: quoted-printable 

text here 

其中「文本這裏」是電子郵件的兩個版本是相同的,但它是不是在實際文件ISN」 t當我嘗試從不同的腳本發送電子郵件。下面的代碼:

def join_transporter(request): 
    form = TransporterJoinForm(request.POST) 

    if request.POST and form.is_valid(): 
     user = User(
      username = form.username.val, 
      first_name = form.first_name.val, 
      last_name = form.last_name.val, 
      email = form.email.val, 
      is_active = False, 
     ) 
     user.set_password(form.password1.val) 
     user.save() 

     Profile.objects.create(
      user = user, 
      phone = form.phone.val, 
      company_name = form.company_name.val, 
      details = Transporter.objects.create(), 
      address = Address.objects.create(
       city = form.address.city.val, 
       province = form.address.province.val, 
       country = form.address.country.val, 
       street = form.address.street.val, 
       postal_code = form.address.postal.val 
      ) 
     ) 

     e = send_multipart_email('Activate Your PROJECT Account', 
      'emails/transporter_joined', 
      {'user':user, 'fee': settings.PROJECT_FEE * Decimal('100.00'),'settings':settings}, 
      [user.email], 
      send_immediately=False 
     ) 
     e.attach_file('/home/PROJECT/webapps/media/files/InsuranceLetter.pdf') 
     e.send() # <------- PROBLEM IS HERE 

     send_multipart_email('Transporter Joined', 
      'emails/staff_transporter_joined', 
      {'trans':user,'settings':settings}, 
      ['[email protected]','[email protected]'] 
     ) 
     messages.info(request, 'Thank you for registering. Please check your email for details on how to activate your account.') 
     return redirect('home') 





def send_multipart_email(subject, template, data_dict, recipient_list, from_email=settings.DEFAULT_FROM_EMAIL, send_immediately=True): 
    if not isinstance(recipient_list, list): recipient_list = [recipient_list] 

    d = {'settings':settings} # default context vars 
    d.update(data_dict) 
    c = Context(d) 

    try: 
     tt = loader.get_template(template+'.txt') 
    except: 
     try: 
      ht = loader.get_template(template+'.html') 
     except: 
      raise Exception('Neither `%(tmpl)s.txt` or `%(tmpl)s.html` could be found.' % {'tmpl':template}) 
     else: 
      e = EmailMultiAlternatives(subject, ht.render(c), from_email, recipient_list) 
      e.content_subtype = 'html' 
    else: 
     e = EmailMultiAlternatives(subject, tt.render(c), from_email, recipient_list) 

     try: 
      ht = loader.get_template(template+'.html') 
     except: 
      pass 
     else: 
      e.attach_alternative(ht.render(c), 'text/html') 

    if send_immediately: 
     e.send() 

    return e 




[[email protected] emails]$ ls 
account_activated.txt  shipper_accepted_bid.txt   transporter_awarded_shipment.txt 
base.html     shipper_joined.html    transporter_bid_declined.html 
base.txt     shipper_joined.txt    transporter_bid_declined.txt 
forgot_password.html  staff_transporter_joined.html transporter_joined.html 
forgot_password.txt  staff_transporter_joined.txt  transporter_joined.txt 
invoice_generated.html  transporter_approved.html  transporter_lost_auction.html 
invoice_generated.txt  transporter_auction_closed.html transporter_lost_auction.txt 
shipper_accepted_bid.html transporter_auction_closed.txt 

票:http://code.djangoproject.com/ticket/13364

+0

哈哈,我得說,我也錯過了。沒有錯別字,它必須是別的東西。 – synic 2010-04-16 20:34:09

+0

我能想到的唯一的其他事情是'ht.render(c)'....模板加載器Gizmo可能會緩存他'.txt'版本,而不是使用新模板更新它? – mpen 2010-04-16 20:38:52

+0

這是什麼版本的Django?曾幾何時,我雖然Django模板加載器採用了'.html'的擴展名,但後來改變了。 – 2010-04-16 20:43:05

回答

3

我不能重新像你所描述的任何(類似拉米羅誰回答的票,並且也不能再現問題)。如果我有兩個具有相同基本名稱,不同擴展名,不同內容的模板,並從代碼中剪切和粘貼以從視圖發送電子郵件,我會在發送的郵件中顯示不同的內容(在Gmail上並檢查「原始」 )。

你說你的.html和.txt文件是不同的,但是根據你所說的一切,它確實聽起來你的.html文件有.txt內容。您已經列出了文件系統和應用程序目錄加載器:哪一個應該是查找這些文件?也許在另一個人看起來有錯誤內容的地方有一個流浪文件?

shell中的一些實驗可能會幫助您進行調試。加載.html模板,看看它是否真的包含你認爲它的作用。例如:

>>> from django.template import loader 
>>> from pprint import pprint 
>>> template = 'emails/dun' 
>>> ht = loader.get_template(template+'.html') 
>>> pprint(ht.nodelist) 
[<Text Node: '<p><strong>Mr. '>, 
<Variable Node: user>, 
<Text Node: '</strong>: Pay us $ '>, 
<Variable Node: amt>, 
<Text Node: ' before next Friday.</p> 
'>] 

驗證html的版本是從.TXT版本不同:

>>> tt = loader.get_template(template+'.txt') 
>>> pprint(tt.nodelist) 
[<Variable Node: user>, 
<Text Node: ': This is an important me'>, 
<Variable Node: amt>, 
<Text Node: '. 
'>] 

如果在加載時一樣,那麼你需要考慮爲什麼正確的模板文件ISN找不到,或者爲什麼找到的內容有錯誤,答案與render()代碼無關。只有當它們在加載時不同並且在渲染時相同纔會查看render()是必要的。

更新:因此根據問題更新,加載的模板是不同的,因爲每個模板擴展了一個不同名稱的基本模板。所以下一個問題就是,這些基本模板是不同的?更改我的娛樂場景以匹配您的場景,至少就原始加載的模板擴展基本模板而言,我仍然無法重新創建問題。我看到不同的模板裝:

>>> from django.template import loader, Context 
>>> from pprint import pprint 
>>> template = 'emails/dun' 
>>> ht = loader.get_template(template+'.html') 
>>> pprint(ht.nodelist) 
[<ExtendsNode: extends "emails/base.html">] 
>>> tt = loader.get_template(template+'.txt') 
>>> pprint(tt.nodelist) 
[<ExtendsNode: extends "emails/base.txt">] 

這使得不同:

>>> c = Context({'user': 'Joe', 'amt': '50.00'}) 
>>> tt.render(c) 
u'\nJoe: This is an important message. You owe us $ 50.00.\n\n' 
>>> ht.render(c) 
u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xm 
lns="http://www.w3.org/1999/xhtml">\n<head>\n<title></title>\n</head>\n<body>\n\n<p><strong>Mr. Joe</strong>: Pay us $ 5 
0.00 before next Friday.</p>\n\n</body>\n</html>\n' 

因爲引用的基本模板有不同的內容:

>>> template = 'emails/base' 
>>> ht = loader.get_template(template+'.html') 
>>> pprint(ht.nodelist) 
[<Text Node: '<!DOCTYPE html PUBLIC "-/'>, 
<Block Node: title. Contents: []>, 
<Text Node: '</title> 
</head> 
<body> 
'>, 
<Block Node: content. Contents: []>, 
<Text Node: ' 
</body> 
</html> 
'>] 
>>> tt = loader.get_template(template+'.txt') 
>>> pprint(tt.nodelist) 
[<Block Node: content. Contents: []>, <Text Node: ' 
'>] 
>>> 
+0

它只是爲每個打印一個擴展節點(請參閱更新),但它們*不同...... – mpen 2010-04-18 18:16:01

1

get_template()查找第一個匹配模板settings.TEMPLATE_LOADERS

TEMPLATE_LOADERS通常是從filesystemapp_directories

app_directories檢查內置所有INSTALLED_APPS爲模板

所以,我會檢查所有的settings.INSTALLED_APPS爲模板,有一個「郵件」文件夾中,並重命名您的模板,或將首先在INSTALLED_APPS列表中選擇一個。

+0

謝謝,但那不是。只有一個'templates'文件夾,一個'emails'文件夾,並且每個這些模板只有一個副本。它不可能將它與錯誤的文件混淆? – mpen 2010-04-29 05:37:19

0

僅供參考,我想我終於算出這是一個Django開發服務器的bug!當我切換到生產環境並在我的apache服務器上運行時,我似乎沒有這個問題。