2016-06-25 12 views
0

Iタプルを持って、その結果タプルで変数を参照するにはどうすればよいですか? (ジャンゴ)

{{VISIBILITY_CHOICES.1.1}} 

出力 "ハロー"。

私は属性「visibility_status」とモデル「タスク」を持っており、ループ内で、task.visibility_status出力のすべての反復が1

{{task.visibility_status}} 

出力1

私はどのように使用しないと言いますこのtask.visibility_statusはタプルのルックアップ内にありますか? VISIBILITY_CHOICES [task.visibility_status] [1]のようなものが別の言語で表示されます。

私は非常にdjangoを新しくしています...ありがとうございます。

編集:私は実行していた コード:

{% for task in tasks %} 
    <div class="post"> 
     <h1><a href="">{{ task.subject }}</a></h1> 
     <div class="date"> 
      <p>Due: {{ task.due_date }}</p> 
      <p>Assigned to: {{task.assigned_to}}</p> 
     </div> 
     <p>{{ task.text_area|linebreaks }}</p> 
     {% with args=""|add:task.visibility_status|add:",1" %} 
     <p>Visibility Status: {{VISIBILITY_CHOICES|get_index:args}}({{ task.visibility_status }})</p> 
     {% endwith %} 
     <p>Case Status: {{ task.case_status }}</p> 
     <div class="date"> 
      <p>Created: {{ task.created_date }} by {{ task.author }}</p> 
     </div> 
    </div> 
{% endfor %} 
+1

カスタムテンプレートフィルタを作成します。参考:[どのように変数をインデックスとしてDjangoテンプレートで使用できますか?](http://stackoverflow.com/questions/13376576/how-can-i-use-a-variable-as-index-in-django-template ) –

答えて

0

組み込み名前tupleは、テンプレート内の任意の構文上の意味を持たないかもしれないが、私は以下のコードでその場所にmy_tupleを使用します。

私はインデックスmy_tuple(すなわちtask.visibility_status1)のための引数を構築するコンテキストを作成するためにwithを使用しました::

{% with x=task.visibility_status|stringformat:"s" %} 
{% with args=x|add:",1" %} 

    {{ my_tuple|get_index:args }} 

{% endwith %} 
{% endwith %} 

そしてcustom template filterで、私は分割さと引数を再作成して使用しましたインデックスのアイテムを返すためにプレーンなPythonでインデックスする:

from django import template 

register = template.Library() 

@register.filter 
def get_index(my_tuple, args): 
    arg1, arg2 = args.split(',') # split on the comma separator 
    try: 
     i = int(arg1) 
     j = int(arg2) 
     return my_tuple[i][j] # same as my_tuple[task.status][1] 
    except: 
     return None 
+0

add:task.statusに問題があります。変数はテンプレートフィルタに渡されません。 "args"を出力すると、 "、1"と表示されます。 {{task.status}}はまだテンプレートに正しく出力されています。 –

+0

私が欲しいものは {%with args = "" | add:{{task.status}} | add: "、1"%} ですが、これはもちろん無効なコードです。 –

+0

'{{task.status}}'は何を表示しますか? '{%%} ' –

関連する問題