i imported a csv and i want to create a table from that. The csv is parsed and update_or_create function help to actualice the database.
the quantities of csv's columns is variable also the rows. Then, (i think) i am forced to do two classic forloops for parse it. The problem is that not working or i am doing something wrong.
the first forloops reads the headers and the second go inside each object.
but i don´t know how to call the attribute of each object
<table >
{% for h in archivo.fieldnames %} // headers are "isbn" and "stock"
{% for x in objeto_nuevo %}
<tr>
<td>{{h}}</td> //render "isbn", ok.
<td>{{{{x}}.{{h}}}}</td> // i want object1.isbn but dont render.
</tr>
{% endfor %}
{% endfor %}
</table>
thanks in advance
More information about variable in template here
<table >
{% for h in archivo.fieldnames %} // headers are "isbn" and "stock"
{% for x in objeto_nuevo %}
<tr>
<td>{{ h }}</td>
<td>{{ x.h }}</td> <!-- Show h attribute of object x -->
</tr>
{% endfor %}
{% endfor %}
</table>
Related
I am trying to do a website to read CSV file and be able to view the data, edit, delete or add new rows/columns. I am able to read the data from a file and view it. I upload a CSV file using a form and then read the data and view it in a page. An example of data looks like this:
Here is the html code for the picture above:
{% extends 'base.html' %}
{% block title %}
Home page
{% endblock %}
{% block content %}
<table class="table">
<tr class="table__header">
<th class="table__cell"> Row </th>
{% for header in data[0] %}
<th class="table__cell"> {{ header }} </th>
{% endfor %}
</tr>
{% for row in data[1:] %}
<tr class="table__row">
<td>{{loop.index}}</td>
{% for cell in row %}
<td class="table__cell"> {{ cell }}</td>
{% endfor %}
<td width="130">
Edit
Delete
</td>
</tr>
{% endfor %}
</table>
{% endblock %}
As you can see I created two buttons, an edit and a delete button for each of the rows. I just do not know how to handle the click for the buttons. If for example I click edit then I want to get redirected to a new page where I can edit the particular row I choose to edit. If instead I click on delete then it will just delete the element. How can I handle the buttons? Can I only handle buttons via forms? or is there other ways? I am very new to Flask (have just programmed in it for 3-4 days) so I have no clue how to do. I tried to search for an awnser but I do not quite understand and I do not get anything similar to my project. Please help.
I would like to display data, three columns per row during my for. I would like my result to look like the following:
<table>
<tr><td>VALUE1</td><td>VALUE2</td><td>VALUE3</td></tr>
<tr><td>VALUE4</td><<td>VALUE5</td><td>VALUE6</td></tr>
</table>
Anyone know how to do it?
Syntax Error
TemplateSyntaxError at /
'for' statements should use the format 'for x in y': for i in range(0, len(all_products_list), 3)
There's a divisibleby tag.
So you can do something (ugly) like:
<table><tr>
{% for field in form %}
<td>{{ field }}</td>
{% if forloop.last %}
</tr>
{% else %}
{% if forloop.counter|divisibleby:"3" %}
</tr><tr>
{% endif %}
{% endif %}
{% endfor %}
</table>
Alternatively, you could give your form class a table_print method that returns a html string (wrapped in mark_safe).
<table>
{% for i in range(0, len(stuff), 3) %}
<tr>
{% for j in range(3) %}
<td>{{ stuff[i+j] }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Sorry misunderstood question.
I want to make table where second element in every row is photo. Like here: http://www.uefa.com/worldcup/season=2014/standings/
I have all my values in list. In list, third element is photo's url. Other variables are just strings. How to write it in Django template?
Here is my code:
<table>
{% for row in data%}
<tr>
{% for value in row %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
I hope you will understand my problem.
Your {{ value }} for that second element should be a path (relative or absolute) to an image resource. If this data is defined by a model within your Django project, make sure the field is set correctly in models.py--it should be a FileField or ImageField. With those, you get a callable URL property. Then in your template, something along these lines:
<table>
{% for row in data%}
<tr>
{% for value in row %}
{% if value.url %}
<td><img src="{{ value.url }}" alt="..."></td>
{% else %}
<td>{{ value }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
I'm fairly new to Django, and am working on a project where I have items appended to multiple lists and would like to display them in a table. I am using the for tag as there is quite a few items in each list. However, when i run my code, the first item on the list repeats over and over, then the second item repeats over and over, and so on. I have a feeling its because i used multiple for tags. Heres my code:
<table>
{% for x in result.netIncomeAr %}
{% for y in result.d2 %}
<tr>
<td>{{ x }}</td>
<td>{{ y }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
Any ideas where I went wrong? Thanks.
The inner loop should use the outer loop variable:
{% for x in result.netIncomeAr %}
{% for y in x.d2 %}
UPD (after looking at the result variable):
You need to change the result variable passed into the template, use zip() to join two lists:
result = zip(df['Date'], df['Net Income'])
return render_to_response('ui/search.html', {"result": result}, context)
Then, in the template iterate over the result this way:
<table>
{% for x in result %}
<tr>
<td>{{ x.0 }}</td>
<td>{{ x.1 }}</td>
</tr>
{% endfor %}
</table>
I would like to display data, three columns per row during my for. I would like my result to look like the following:
<table>
<tr><td>VALUE1</td><td>VALUE2</td><td>VALUE3</td></tr>
<tr><td>VALUE4</td><<td>VALUE5</td><td>VALUE6</td></tr>
</table>
Anyone know how to do it?
Syntax Error
TemplateSyntaxError at /
'for' statements should use the format 'for x in y': for i in range(0, len(all_products_list), 3)
There's a divisibleby tag.
So you can do something (ugly) like:
<table><tr>
{% for field in form %}
<td>{{ field }}</td>
{% if forloop.last %}
</tr>
{% else %}
{% if forloop.counter|divisibleby:"3" %}
</tr><tr>
{% endif %}
{% endif %}
{% endfor %}
</table>
Alternatively, you could give your form class a table_print method that returns a html string (wrapped in mark_safe).
<table>
{% for i in range(0, len(stuff), 3) %}
<tr>
{% for j in range(3) %}
<td>{{ stuff[i+j] }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Sorry misunderstood question.