【问题标题】:Comma separating a list of dictionary items using jinja template逗号分隔使用 jinja 模板的字典项目列表
【发布时间】:2021-12-29 08:23:39
【问题描述】:

我有一个需要用逗号分隔的字典列表,最后一项除外。 例如,字典列表应该是这样的:

education_details[{degree: "B.S.", type: "Full-type", location:"aus",grade:"A"}, {degree: "MCA", type: "Full-type", location:"aus",grade:"B"}]

我在我的 jinja 模板中呈现上述细节(education_details),如下所示:

{% for data in profile.get('education_details', []) %}
  {% if data.get('degree', '') %}
    {% if not loop.last %}
       <span>{{data.get('degree', '')}},</span>
    {% else %}
       <span>{{data.get('degree', '')}}</span>
    {% endif %}
  {% endif %}

  {% if data.get('type', '') %}
    {% if not loop.last %}
       <span>{{data.get('type', '')}},</span>
    {% else %}
       <span>{{data.get('type', '')}}</span>
    {% endif %}
  {% endif %}

  {% if data.get('location', '') %}
    {% if not loop.last %}
       <span>{{data.get('location', '')}},</span>
    {% else %}
       <span>{{data.get('location', '')}}</span>
    {% endif %}
  {% endif %}
{% endfor %}

我尝试使用 last,它不起作用

预期 o/p:

逗号出现在每个字段之后,最后输入的项目除外

degree
degree, type, location
degree, location

【问题讨论】:

    标签: python loops flask jinja2


    【解决方案1】:

    根据您的示例数据和预期的 o/p,我假设您想要这样的输出:

    B.S., Full-type, aus 
    MCA, Full-type, aus 
    

    如果是这种情况,您需要逐行打印每个列表项,并逐个打印每个字典项的值,由, 分隔,最后一个值除外。

    如果是这样,则根本不需要使用loop.last,而是只需检查每个字典键的顺序即可。下面是代码 sn-p (flask + jinja2):

        template = """
    {% for data in profile.get('education_details', []) %}
    
      {% if data.get('degree', '') %}
           <span>{{data.get('degree', '')}},</span>
      {% endif %}
    
      {% if data.get('type', '') %}
           <span>{{data.get('type', '')}},</span>
      {% endif %}
    
      {% if data.get('location', '') %}
           <span>{{data.get('location', '')}}</span>
      {% endif %}
    
      <br/>
    
    {% endfor %}
    
        """
        education_details = [{"degree": "B.S.", "type": "Full-type", "location":"aus","grade":"A"},
                             {"degree": "MCA", "type": "Full-type", "location":"aus","grade":"B"}]
    
        return render_template_string(template, profile={"education_details": education_details})
    

    【讨论】:

      【解决方案2】:

      模板:

      {%- set listkeys = ['degree', 'type', 'location'] %}
      {%- for data in profile.get('education_details', []) %}
        {%- set nbr = namespace(value=0) %}
        {%- for k in listkeys %}
        {%- if data.get(k, '') %}{%- set nbr.value = nbr.value + 1 %}{%- endif %}
        {%- endfor %}
        {%- for k in listkeys %}
          {%- if data.get(k, '') %}
              {%- set nbr.value = nbr.value - 1 %}
                  {%- if nbr.value > 0 %}
                      <span>{{data.get(k, '')}},</span>
                  {%- else %}
                      <span>{{data.get(k, '')}}</span>
                  {%- endif %}       
          {%- endif %}
        {%- endfor %}
        <br/>
      {%- endfor %}
      

      游戏测试:

      education_details = [{"degree": "B.S.", "location":"aus","grade":"A"},
                           {"degree": "B.S."},
                           {"degree": "B.S.","grade":"A"},
                           {"degree": "B.S.", "type": "Full-type","grade":"A"},
                           {"type": "Full-type", "grade": "A"},
                           {"degree": "MCA", "type": "Full-type", "location":"aus","grade":"B"}]
      

      结果:

                      <span>B.S.,</span>
                      <span>aus</span>
        <br/>
                      <span>B.S.</span>
        <br/>
                      <span>B.S.</span>
        <br/>
                      <span>B.S.,</span>
                      <span>Full-type</span>
        <br/>
                      <span>Full-type</span>
        <br/>
                      <span>MCA,</span>
                      <span>Full-type,</span>
                      <span>aus</span>
        <br/>
      

      编辑 没有namespace的模板:

      {%- set listkeys = ['degree', 'type', 'location'] %}
      {%- for data in profile.get('education_details', []) %}
        {%- set nbr = {'value': 0} %} 
        {%- for k in listkeys %}
        {%- if data.get(k, '') %}{%- set _ = nbr.update({'value': nbr.value + 1}) %}{%- endif %}
        {%- endfor %}
        {%- for k in listkeys %}
          {%- if data.get(k, '') %}
              {%-  set _ = nbr.update({'value': nbr.value - 1}) %}
                  {%- if nbr.value > 0 %}
                      <span>{{data.get(k, '')}},</span>
                  {%- else %}
                      <span>{{data.get(k, '')}}</span>
                  {%- endif %}       
          {%- endif %}
        {%- endfor %}
        <br/>
      {%- endfor %}
      

      你可以使用宏:

      {%- macro inc(dct, key, inc=1) -%}
          {%- if dct.update({key: dct[key] + inc}) %}{%- endif -%}
      {%- endmacro -%}
      
      {%- set listkeys = ['degree', 'type', 'location'] %}
      {%- for data in profile.get('education_details', []) %}
        {%- set nbr = {'value': 0} %} 
        {%- for k in listkeys %}
        {%- if data.get(k, '') -%}{{ inc(nbr, 'value', 1) }}{%- endif %}
        {%- endfor %}
        {%- for k in listkeys %}
          {%- if data.get(k, '') -%}
              {{ inc(nbr, 'value', -1) }}
                  {%- if nbr.value > 0 %}
                      <span>{{data.get(k, '')}},</span>
                  {%- else %}
                      <span>{{data.get(k, '')}}</span>
                  {%- endif %}       
          {%- endif %}
        {%- endfor %}
        <br/>
      {%- endfor %}
      

      【讨论】:

      • Getting NameError: name 'namespace' is not defined(我使用的是 3.6)
      • 我认为它与 python 版本无关,但 jinja2.. 如果命名空间不起作用,我修复可能会用其他解决方案回答
      猜你喜欢
      • 1970-01-01
      • 2014-08-01
      • 2012-08-12
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 2013-07-03
      • 2016-09-27
      • 2021-12-31
      相关资源
      最近更新 更多