2017-05-24 9 views
0

私はすべてのスタックを見てきました。私は何もできません。Django - コンテキスト変数

わかりやすいのは、forループ内に再割り当てする必要があるコンテキスト変数がありますが、新しい繰り返しが開始されると、変数が元のコンテキスト値にリセットされます。だから私の質問は、どのように私は割り当てられた値をループ全体に永続化しておくのですか?

私はこのような値を代入しています:コードの完全な抜粋は以下の

{% with task.new_stage as current %}{% endwith %} 

{% for task in tasks_list %} 
<h3>At Beg {{current}}</h3> 
{% if task.project_stage.id != current %} 
<div id="tasks-container-{{ task.project_stage.id }}" class="task-container btn-group-vertical col-md-3"> 
{% endif %} 
    <div class="panel my-panel tasks-panel col-md-11 {% if task.project_stage.has_started and not task.project_stage.has_ended %}active{% endif %} "> 
     <span class="text-uppercase">{{ task.task_name }}</span> 
     <div class="col-md-12"> 
      <img id="task-{{ task.id }}" class="download-pdf" src="{% static 'dpp/images/cloud.png' %}" /> 
     </div> 
    </div> 
{% if task.project_stage.id != current %} 
</div>{% with task.new_stage as current %}<h3>At End{{current}}</h3>{% endwith %} 
{% endif %} 
{% endfor %} 

ビュー:

def get(self, request): 
    if not request.user.is_authenticated(): 
     form = self.form_class(None) 
     return render(request, 'dpp/login.html', {'form': form}) 

    tasks_list = ProjectTask.objects.all() 
    context = { 
     'tasks_list' : tasks_list, 
     'current' : 0, 
    } 
    return render(request, self.template_name, context) 

答えて

2

{% with %}のコンテキストが{% endwith %}まで継続。ブロックを開始した直後にブロックを終了するので、文字通り効果はありません。

コンテキスト内で変数を再割り当てしようとしないでください。最後の繰り返し以降に何かが変更されたかどうかだけを確認したい場合は、特別に設計されたテンプレートタグを使用する必要があります:ifchanged

+0

これでDjangoについてもう少し詳しく説明します。それはwithsです:P ありがとうございます!私は今、私が望むことをすることができました –

関連する問題