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