render_to_string()抱怨NoneType错误

时间:2015-07-03 13:15:03

标签: python django

所以我在python manage.py shell测试我的代码,但是我收到了以下错误:AttributeError: 'NoneType' object has no attribute 'find'

以下是我在应用models.py中所做的事情:

from djmoney.models.fields import MoneyField
from django.template.loader import render_to_string
from django.db import models
from datetime import date
import mandrill

< ...>

def email_invoice(self):
    dated, amount, subtenant = self.dated, self.amount, self.subtenant.name
    net_amount = self.amount / 1.13
    hst = amount - net_amount
    context = {
                'date': dated,
                'amount': amount,
                'net_amount': net_amount,
                'hst': hst,
                'subtenant': subtenant
    }
    msg_html = render_to_string('email/invoice.html', context)
    message = {
                'to': [{'email': '<left out>',
                        'name': '<left out>',
                        'type': 'to'}],
                'subject': 'Testing',
                'html': msg_html,
                'from_email': '<left out>',
                'from_name': '<left out>'
    }
    try:
        mandrill_client = mandrill.Mandrill('<left out>')
        result = mandrill_client.messages.send(message=message)
    except mandrill.Error as e:
        print('A mandrill error occurred: %s - %s' % (e.__class__, e))
        raise

回溯似乎表明问题出在render_to_string(),但我不知道我的实施方式出了什么问题:我测试了模板是否存在(它&#39; shell中的电子邮件模板)。实际上,没有上下文的render_to_string('email/invoice.html')可以正常工作。问题在于背景。

我尝试在shell中使用简单的dict,比如context = {'hst': 123},这也有效。出于某种原因,它不喜欢上面的dict,并抱怨NoneType。我单独检查每个对象字段以确保它存在并且它在shell中返回一个值。

知道这个NoneType来自何处?

编辑:这是完整的追溯

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/cyehia/Documents/Apps/gcap/rental/models.py", line 41, in email_invoice
    msg_html = render_to_string('email/invoice.html', context)
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/django/template/loader.py", line 99, in render_to_string
    return template.render(context, request)
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/django/template/backends/django.py", line 74, in render
    return self.template.render(context)
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/django/template/base.py", line 209, in render
    return self._render(context)
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/django/template/debug.py", line 79, in render_node
    return node.render(context)
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/django/template/debug.py", line 92, in render
    output = force_text(output)
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/django/utils/encoding.py", line 90, in force_text
    s = six.text_type(s)
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/djmoney/models/fields.py", line 133, in __str__
    locale = self.__get_current_locale()
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/djmoney/models/fields.py", line 103, in __get_current_locale
    locale = translation.to_locale(translation.get_language())
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 185, in to_locale
    return _trans.to_locale(language)
  File "/Users/cyehia/Documents/Apps/venv/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 75, in to_locale
    p = language.find('-')
AttributeError: 'NoneType' object has no attribute 'find'

1 个答案:

答案 0 :(得分:0)

问题似乎是incompatibly between Django 1.8 and django-money。我尝试手动激活语言:

from django.utils.translation import activate 

def email_invoice(self):
    <....>
    activate('en')
    msg_html = render_to_string('email/invoice.html', context)