【问题标题】:Decode values of a tuple in django template在 django 模板中解码元组的值
【发布时间】:2011-04-06 20:09:33
【问题描述】:
  if(len(f1) > 0):  
    for qs in profile_map:
        p = Profile.objects.get(pk=qs.emp.id)
        t_name = p.first_name + p.last_name
        t_arr.append((q.profile.id,emp_name))
    response_dictionary.update({'tarr':t_arr})
  render_to_response('project/profile_table.html',context_instance=RequestContext(request,{'response_dictionary': response_dictionary}))

在 Django 模板中如何解码 1. 元组的所有值 2. 在元组中搜索 q.profile.id 中的某个值

        {% for ele in response_dictionary.tarr%}
            alert('{{ele}}');
        //Get this as alert (11L, u'Employee3.')
         {% endfor %}  

【问题讨论】:

    标签: python django django-views django-templates


    【解决方案1】:

    在您的情况下,生成器会将元组分配给ele,因此您可以使用{{ ele.0 }} {{ ele.1 }} 访问名字和姓氏。

    但这也是合法的,将元组解包成两个变量:

    {% for first_name, last_name in response_dictionary.tarr %}
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 django 0.96,则 for 循环中不能有多个值。所以这不起作用:

      {% for first_name, last_name in response_dictionary.tarr %}
      

      改为使用

      {% for ele in response_dictionary.tarr %}
           {{ ele.0 }} {{ ele.1 }}
      {% endfor %}
      

      【讨论】:

        猜你喜欢
        • 2011-07-10
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        • 2019-05-29
        • 1970-01-01
        • 2016-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多