Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

python - getting list from dictionary in jinja

I get such list:

positions = [
      {
        "title": "Head of web development department",
        "salary": "180,000 - 200,000",
        "level": "lead",
        "tags": ["OOP", "Git", "Flask", "Redis", "Project Management", "People Management",
                 "Team Building", "Negotiating"],
      }
    ]

I need to get html text:
Head of Web Development OOP GIT

My code is:

   {% for k in positions %}
        <div>
            <h3>{{ k.title }}</h3>
            <p>{{k.salary}}</p>
            {% for tags_inside in {{k.tags}} %}
                <ul>  {{tags_inside}}</ul>
            {% endfor %}
        </div>
    {% endfor %}

And I get an error:

line 43, in template
    {{k.tags}{1}}
jinja2.exceptions.TemplateSyntaxError: unexpected '}'
127.0.0.1 - - [24/Jan/2021 17:36:45] "GET /data HTTP/1.1" 500 -

How should I solve this problem?

question from:https://stackoverflow.com/questions/65872467/getting-list-from-dictionary-in-jinja

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
from jinja2 import Template

positions = [
      {
        "title": "Head of web development department",
        "salary": "180,000 - 200,000",
        "level": "lead",
        "tags": ["OOP", "Git", "Flask", "Redis", "Project Management", "People Management",
                 "Team Building", "Negotiating"],
      }
    ]

templ  = Template("""
  {% for k in positions %}
        <div>
            <h3>{{ k.title }}</h3>
            <p>{{k.salary}}</p>
            <ul>
            {% for tags_inside in k.tags %}
                <li>{{tags_inside}}</li>
            {% endfor %}
            </ul>
        </div>
    {% endfor %}
""")

print(templ.render(positions=positions))

output

        <div>
            <h3>Head of web development department</h3>
            <p>180,000 - 200,000</p>
            <ul>
            
                <li>OOP</li>
            
                <li>Git</li>
            
                <li>Flask</li>
            
                <li>Redis</li>
            
                <li>Project Management</li>
            
                <li>People Management</li>
            
                <li>Team Building</li>
            
                <li>Negotiating</li>
            
            </ul>
        </div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...