0

I have a list which contains different information. I want to print them out so I did this:

<tr>
{% for item in aList %}
    <td class="dateIndex">
        <div>Date: {{item[0]}}</div>
        <div>Price: {{item[1]}}</div>
        <div>Status: {{item[2]}}</div>
    </td>
{% endfor %}
</tr>

The problem is that all items go to a single line and the output looks like this:

Date: 1 200 available
Price:
Status:

When I'd like it to be:

Date: 1
Price: 200
Status: available

Any tips are appreciated.

EDIT: aList contains items from a CSV file which looks like this enter image description here

@app.route('/rent')
def rent():
    aList = readFile('static\\dates.csv')
    return render_template('rent.html', aList = aList)

def readFile(aFile):
    with open(aFile, 'r') as inFile: 
        reader = csv.reader(inFile)
        file = [row for row in reader]
    return file
RnD
  • 1,019
  • 5
  • 23
  • 49

3 Answers3

1
def readFile(aFile):
    with open(aFile, 'r') as inFile: 
        reader = csv.reader(inFile)
        data = [row[0].split('\t') for row in reader]
    return data

First of all you should not use file as a variable name to hold your data as its a built-in type.

>>> file
<type 'file'>
>>>
>>> type(file)
<type 'type'>

Second it would be better if you would split it while reading your data from csv file instead of splitting it on template which looks ugly plus will break your template if you get some anonymous data.

<tr>
{% for item in aList %}
    <td class="dateIndex">
        <div>Date: {{item.0}}</div>
        <div>Price: {{item.1}}</div>
        <div>Status: {{item.2}}</div>
    </td>
{% endfor %}
</tr>

You should use dot opertator to point the index at template level.

Tanveer Alam
  • 5,185
  • 4
  • 22
  • 43
0

I think you have two issues here.

First, you will probably need to create a tr for every result

{% for item in aList %}
<tr>
    <td class="dateIndex">
        <div>Date: {{item.0}}</div>
        <div>Price: {{item.1}}</div>
        <div>Status: {{item.2}}</div>
    </td>
</tr>
{% endfor %}

Second, you are probably experiencing some browser issue with divs appearing in td's.

At the very least, you should ensure you divs are displaying as block level elements and not inline....

JackDev
  • 4,891
  • 1
  • 39
  • 48
0

Found a solution finally.

it kept the item as a list ['1\t200\tavailable'] so splitting everything in between \t with .split() worked perfectly

<div>Date: {{item[0].split('\t')[0]}}</div>
<div>Price: {{item[0].split('\t')[1]}}</div>
<div>Status: {{item[0].split('\t')[2]}}</div>
RnD
  • 1,019
  • 5
  • 23
  • 49
  • I asked you above whether you are getting comma seperated list but it seems you were getting space tab between vales. – Tanveer Alam Nov 30 '14 at 18:51