django template turn array into HTML table

后端 未结 3 1039
悲哀的现实
悲哀的现实 2021-02-02 17:32

I have a list of 16 results, let\'s call it \"results\". I want to arrange them in a 4 x 4 table.

Using the django template, how can I do this? (It doesn\'t seem like cy

3条回答
  •  遥遥无期
    2021-02-02 17:49

    You need to build something like this

    
    {% for r in result %}
     
    {% endfor %}
    
    header1 header2 header3 header4
    {{ result.name }} {{ result.address }} {{ result.time }} {{ result.date }}

    provided that you have an array (actually, a dictionary) this way

    result['name']
    result['address']
    result['time']
    result['date']
    return render_to_response("my_template.html", {'result:result'})
    

    There are a number of do this. This is the most straightforward ways. Look at Django template tag documentation.

    Here is a list of techniques I learned throughout. There are more, but I don't have time to document all of them. http://binarybugs01.appspot.com/entry/template-iteration-techniques

    Sometimes you have to be careful with the context dictionary you are passing to the template. If you are passing this

    result = {'name': 'John', 'time': '12/2/2012'....etc}
    context['result'] = result
    return render_to_response("my_template.html", context}
    

    You are iterating over result.result and the keys are result.result.name


    I also want to remind you that you either have a list, a set, a dictionary, or a tuple.You can import array and use it, however.

提交回复
热议问题