2017-12-04 7 views
0

モデルCoperと、価格の引用符を動的に取得する必要があるかどうかを選択するフィールドがあります。したがって、ユーザーが「動的」を選択すると、ユーザーは製品および他の料金の見積もりを取得します。TemplateTagを使用した計算の処理なし

私は商品IDを取得し、売り手の通貨をフィルターに掛け、別のモデルのレートを検索して見積もりを完成させるテンプレートタグを書いた。

{% load quote_filter %} 
    {% for m in all_quotes %} 
     {% if m.pricing == 'dynamic' %} 
      {{m.id|get_total_quote }} 
     {% else %} 
      <p> Fixed price is $3500 </p> 
     {% endif %} 
    {% empty %} 
     <p> No product yet. </p> 
    {% endfor %} 

私はサイトをロード

@register.filter(name='get_total_quote') 
def get_total_quote(value): 
    tap = Coper.objects.get(pk=value) 
    get_cnd = VAM.objects.get(money_code = tap.seller.profile.currency) 
    ratex = get_cnd.money_rate 
    if tap.margin_type == 'plus': 
     percent = tap.margin_figure/100 
     addup = ratex * percent 
     total_amt = ratex + addup 
     return total_amt 
    if tap.margin_type == 'minus': 
     percent = tap.margin_figure/100 
     addup = ratex * percent 
     total_namt = ratex - addup 
     return total_namt 

テンプレートは、私は、ダイナミックな引用符の代わりに、実際の数字の価格としてNONEを得ませんでした。

私には何が欠けていますか?

+3

追加いくつかの印刷やテンプレートタグへのロギング。 'margin_type'が' 'plus''でも' 'minus''でもない場合、テンプレートタグは' None'を返します。 – Alasdair

+1

また、なぜIDだけをタグに渡してから、Coperに問い合わせるのですか? 「m」はすでにCoperオブジェクトではないのですか? –

+0

@Alasdairはいそうです。どのように私はこれを逃したのか分からない!今すぐうまくいく!ありがとう。あなたをマークアップする答えとして投稿してください。 – YoYo

答えて

1

if文がTrueでない場合、タグはNoneを返します。

if tap.margin_type == 'plus': 
    ... 
    return total_amt 
if tap.margin_type == 'minus': 
    ... 
    return total_amt 
# if code gets to this point, you are implicitly returning None 

ダニエルはコメントで示唆したように、あなたがタグに直接Coperオブジェクトを渡すことができます。

{{m|get_total_quote }} 

その後、tapを受け入れ、tap = Coper.objects.get(pk=value)を削除するには、フィルタを変更します。

@register.filter(name='get_total_quote') 
def get_total_quote(tap): 
    get_cnd = VAM.objects.get(money_code = tap.seller.profile.currency) 
    ... 
関連する問題