2011-07-29 63 views
0

我正在使用templatetag從我寫的公式返回變量。我一直在試圖找出爲什麼我得到這個回溯。我已經在shell中測試了公式,並且所有工作都正常。我在標籤和視圖中做了一些打印語句。但無法弄清楚這個原因。不支持的操作數類型爲+:'int'和'unicode'

TRACEBACK:

Traceback: 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response 
    117.        response = middleware_method(request, e) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response 
    111.       response = callback(request, *callback_args, **callback_kwargs) 
File "/Users/urbanrunic/developement/levelend/levelend/levelend/configs/common/../../apps/common/utils/decorators.py" in wrapper 
    65.       context_instance=RequestContext(request), mimetype=mimetype) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/shortcuts/__init__.py" in render_to_response 
    20.  return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/loader.py" in render_to_string 
    188.   return t.render(context_instance) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in render 
    123.    return self._render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in _render 
    117.   return self.nodelist.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in render 
    744.     bits.append(self.render_node(node, context)) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/debug.py" in render_node 
    73.    result = node.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/loader_tags.py" in render 
    127.   return compiled_parent._render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in _render 
    117.   return self.nodelist.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in render 
    744.     bits.append(self.render_node(node, context)) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/debug.py" in render_node 
    73.    result = node.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/loader_tags.py" in render 
    127.   return compiled_parent._render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in _render 
    117.   return self.nodelist.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in render 
    744.     bits.append(self.render_node(node, context)) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/debug.py" in render_node 
    73.    result = node.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django_static/templatetags/django_static.py" in render 
    108.   code = self.nodelist.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in render 
    744.     bits.append(self.render_node(node, context)) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/debug.py" in render_node 
    73.    result = node.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/loader_tags.py" in render 
    64.    result = block.nodelist.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in render 
    744.     bits.append(self.render_node(node, context)) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/debug.py" in render_node 
    73.    result = node.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/loader_tags.py" in render 
    64.    result = block.nodelist.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in render 
    744.     bits.append(self.render_node(node, context)) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/debug.py" in render_node 
    73.    result = node.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/defaulttags.py" in render 
    311.    return self.nodelist_true.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/base.py" in render 
    744.     bits.append(self.render_node(node, context)) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/debug.py" in render_node 
    73.    result = node.render(context) 
File "/Users/urbanrunic/.virtualenvs/levelend/lib/python2.6/site-packages/django/template/defaulttags.py" in render 
    227.     nodelist.append(node.render(context)) 
File "/Users/urbanrunic/developement/levelend/levelend/levelend/configs/common/../../apps/lender/templatetags/lender_tags.py" in render 
    15.   context[self.min] = monthlyPayment(self.how_much, self.apr_min, self.max_repayment_length) 
File "/Users/urbanrunic/developement/levelend/levelend/levelend/configs/common/../../apps/lender/utils.py" in monthlyPayment 
    34.  M = P/((1 - 1/(1 + mi) ** n)/mi) 

Exception Type: TemplateSyntaxError at /results/ 
Exception Value: Caught TypeError while rendering: unsupported operand type(s) for +: 'int' and 'unicode' 

公式:

def monthlyPayment(P, mi, n): 
    """ returns monthly payment """ 
    M = P/((1 - 1/(1 + mi) ** n)/mi) 
    return M 

TAG:

class MinMonthlyPaymentsNode(Node): 
    def __init__(self, how_much, apr_min, max_repayment_length, min): 
     self.how_much, self.apr_min, self.max_repayment_length, self.min = how_much, apr_min, max_repayment_length, min 

    def render(self, context): 
     context[self.min] = monthlyPayment(self.how_much, self.apr_min, self.max_repayment_length) 
     return '' 

def do_monthly_payment(parser, token): 
    """ 
    returns a monthly payment based on the formula with interst and amount of loan 

    Example usage: 
    {% min_monthly_payments how_much, apr_min, max_repayment_length as [min]%} 
    """ 
    args = token.contents.split() 
    if len(args) != 6: 
     raise TemplateSyntaxError, "min_monthly_payments tag takes exactly six arguments" 
    if args[4] != 'as': 
     raise TemplateSyntaxError, "fourth argument to the min_monthly_payments tag must be 'as'" 

    return MinMonthlyPaymentsNode(args[1], args[2], args[3], args[5]) 

register.tag('min_monthly_payments', do_monthly_payment) 

中號ODELS:

class Lender(TitleAndSlugModel): 
    """ 
    A lender 
    """ 

    DISBURSED_TYPES = get_namedtuple_choices('DISBURSED_TYPES', (
     (0, 'DIRECT_TO_SCHOOL', 'Directly to School'), 
     (1, 'SENT_TO_YOU', 'Sent to You'), 
    )) 
    SUITABILITY_TYPES = get_namedtuple_choices('SUITABILITY_TYPES', (
     (0, 'UNDERGRADUATE', 'Undergraduate'), 
     (1, 'GRADUATE', 'Graduate'), 
     (2, 'LAW', 'Law'), 
     (3, 'MEDICAL', 'Medical'), 
     (4, 'PARENTS', 'Parents'), 
    )) 

    # Financial summary 
    apr_min = models.DecimalField(max_digits=5, decimal_places=2) 
    apr_max = models.DecimalField(max_digits=5, decimal_places=2) 

    # Borrower rewards 
    auto_debit_discount = models.DecimalField(max_digits=5, decimal_places=2) 
    auto_debit_discount_note = models.CharField(max_length=250, null=True, blank=True) 
    additional_rewards_1 = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True) 
    additional_rewards_1_note = models.CharField(max_length=250, null=True, blank=True) 
    additional_rewards_2 = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True) 
    additional_rewards_2_note = models.CharField(max_length=250, null=True, blank=True) 

    # General info 
    loan_name = models.CharField(max_length=250) 
    loan_services_by = models.CharField(max_length=250) 
    funds_disbursed = models.PositiveIntegerField(choices=DISBURSED_TYPES.get_choices(), default=0) 
    co_signer_release_available = models.IntegerField(null=True, blank=True, help_text="Please enter in months") 
    suitable_for = MultiSelectField(max_length=50, choices=SUITABILITY_TYPES.get_choices()) 
    external_url = models.URLField(max_length=250) 
    lender_logo = FileBrowseField("Image", max_length=200, directory="uploads/", extensions=[".jpg"], blank=True, null=True) 
    schools = models.ManyToManyField(School) 

    # Repayment 
    max_repayment_length = models.IntegerField(null=True, blank=True, help_text="Please enter in years") 
    max_deferral_length = models.IntegerField(null=True, blank=True, help_text="Enter as X months after graduation") 

    # Rates 
    interest_rate_index = models.DecimalField(max_digits=5, decimal_places=2) 
    interest_rate_index_note = models.CharField(max_length=250, null=True, blank=True) 
    min_repayment_rate = models.DecimalField(max_digits=5, decimal_places=2) 
    min_repayment_rate_note = models.CharField(max_length=250, null=True, blank=True) 
    max_repayment_rate = models.DecimalField(max_digits=5, decimal_places=2) 
    max_repayment_rate_note = models.CharField(max_length=250, null=True, blank=True) 

    # Fees 
    origination_fee = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True) 
    guarantee_fee = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True) 

    # Notes 
    notes = models.TextField(null=True, blank=True) 

    def get_absolute_url(self): 
     return reverse("results_detail", args=[self.slug]) 

    def __unicode__(self): 
     return "%s" % (self.title) 

再次任何幫助將不勝感激,哪怕是朝着正確方向的一個點。謝謝!

+4

如果您的問題是關於回溯,請包含回溯。 –

+0

你可以編輯你的問題,包括實際的回溯? – SingleNegationElimination

+0

對不起,已添加追蹤編輯。 – tjoenz

回答

1

回溯將表明,「MI」在公式中的參數是一個unicode字符串,而不是一個號碼代替上述行(整數或浮點數)。我不能告訴ABOU Django,但也許token.contents.split()返回一個字符串列表,你必須轉換;您可以自己檢查在monthlyPayment函數中添加一些調試打印。

1

假設args是一個字符串數組,正如它通常那樣,您將字符串值傳遞到需要數字的函數中。

return MinMonthlyPaymentsNode(args[1], args[2], args[3], args[5]) 

然後,它試圖與他們做數學計算,這是行不通的,因爲Python不會自動轉換它們。如果所有這些論點應該是浮點數,我會

return MinMonthlyPaymentsNode(float(args[1]), float(args[2]), float(args[3]), float(args[5])) 
+0

謝謝,我試過這個,但得到了這個回溯: do_monthly_payment中的文件「/Users/urbanrunic/developement/levelend/levelend/levelend/configs/common/../../apps/lender/templatetags/lender_tags.py」 32.返回MinMonthlyPaymentsNode(float(args [1]),float(args [2]),float(args [3]),float(args [5])) 異常類型:ValueError at/results/ 異常值:float()的無效文字:how_much, – tjoenz

相關問題