django中过滤器中的默认值

时间:2013-11-25 07:28:03

标签: django django-templates django-template-filters

我有这样的观点:

if request.GET.has_key('ic_iteration') and request.GET['ic_iteration'] != 'all':
    # Filter with ic_iteration
    iteration = request.GET['ic_iteration']
    context['iteration'] = request.GET['ic_iteration']
    persons = Person.objects.filter(organisation__name="The Interaction Consortium").filter(assemblaticket__ic_iteration=iteration).annotate(estimate_total=Sum('assemblaticket__estimate'))
    context['persons'] = persons
    context['iciteration'] = ICIteration.objects.all()
    return render_to_response("willard/persons_ic.html", context)     
else:
    # Default
    iteration = ICIteration.objects.get_current()
    context['iteration'] = iteration
    # get all persons related to the interation consortium with total of all estimates for all tickets
    persons = Person.objects.filter(organisation__name="The Interaction Consortium").filter(assemblaticket__ic_iteration=iteration).annotate(estimate_total=Sum('assemblaticket__estimate'))
    context['persons'] = persons
    context['iciteration'] = ICIteration.objects.all()
    return render_to_response("willard/persons_ic.html", context)

在模板中:

IC Iteration: <select name="ic_iteration">
<option value="all">--All--</option>
  {% for ic_iteration in iciteration %}
  <option value="{{ ic_iteration.id }}" {% if iteration == ic_iteration.id|stringformat:"s" %}selected="selected"{% endif %}>{{ ic_iteration.name }}</option>
  {% endfor %}
</select> 

当我第一次访问该页面时,它会转到else语句并传递迭代上下文。但无论选择选项在-All-显示什么,但如果我稍后更改过滤器并点击应用{% if iteration == ic_iteration.id|stringformat:"s" %}selected="selected"{% endif %}就可以了。怎么了?

0 个答案:

没有答案