I am using Python FLask to build a webapp and I am trying to push a nested list to a table in HTML.
Python
item = [[22-03-20, $1409.50, 22-03-20], [22-03-20, $60.00, 22-03-20]]
current HTML
{% for rows in item %}
<tr>
<th scope="row">•</th>
{% for cell in rows %}
<td>{{cell}}</td>
{% endfor %}
<td>edit</td>
</tr>
{% endfor %}
The reason for a duplicate of dates in the nested list is because I need to create a special route page to the date itself.
Output I want to achieve
Basically, the edit will redirect to the page to the respective date. As I have multiple expenses of the same date, I can't just use the unique id within the table.
PROBLEM
The current HTML I have is able to output the 3 items I need, date, expense, date but I can't put a URL redirect to the 3rd variable, date. Is there a way to have 2 for loops running parallel to each other, so I could go through 2 lists at the same time? or is there a better way to do what I want to achieve?
Found a way looking through the documentation again, don't need a parallel for loop actually. I changed the nested list to a tuple in a list. Not sure if this is the best way to do it though.
Python
item = [(22-03-20, $1409.50, 22-03-20), (22-03-20, $60.00, 22-03-20)]
HTML
{% for x,y,z in item %}
<tr>
<th scope="row">•</th>
<td>{{x}}</td>
<td>{{y}}</td>
<td>edit</td>
</tr>
{% endfor %}
I think you should use a list of maps.
item = [
{
'date':'22-03-20',
'price':'$1409.50',
'href':'URI'
},
{
'date':'22-03-20',
'price':'$60.00',
'href':'URI'
},
]
so,
{% for row in item %}
<tr>
<th scope="row">•</th>
<td>{{row.date}}</td>
<td>{{row.price}}</td>
<td>
edit
</td>
</tr>
{% endfor %}
Related
Good day,
I'm asking myself, if it's possible to transfer multiple informations as primary-keys inside my template!?
For example, when clicking on a link inside my table...
In this case im transfering the item-id:
<tbody>
{% for item in dataset %}
<tr>
<td>
Item-Name
</td>
</tr>
{% endfor %}
</tobdy>
Now I want to transfer the id and - let's say - the name! Is something like this even possible?
<tbody>
{% for item in dataset %}
<tr>
<td>
Item-Name
</td>
</tr>
{% endfor %}
</tobdy>
And if it's possible, do I have to chage something inside my urls.py? Right now it's looking like this:
path('index/<str:pk>', views.example, name="Example"),
Thanks for all your help and a great day!
Your url should be
{% url 'Examplepage' id=item.id name=item.name %}"
And your path should be
path('index/<str:id>/<str:name>/', views.example, name="Example"),
I have followed the code here https://learn.microsoft.com/en-us/outlook/rest/python-tutorialnd and it works however I need to access the calender events for my own purpose.
context = { 'events': events['value'] }
return render(request, 'tutorial/events.html', context)
The HTML is
<table class="table">
<tr>
<th>Subject</th>
<th>Start</th>
<th>End</th>
</tr>
{% for event in events %}
<tr>
<td>{{ event.subject }}</td>
<td>{{ event.start.dateTime }} ({{ event.start.timeZone }})</td>
<td>{{ event.end.dateTime }} ({{ event.end.timeZone }})</td>
</tr>
{% endfor %}
</table>
My question is how can I interrogate context to obtain the data in the format shown above? Ie subject start date time and end date time.
I'm very new to Python.
I've seen how the data is held using debug statements.
See above
I need interrogate context to obtain the data in the format shown above? Ie subject start date time and end date time.
The data in Context looks like this
context data
How does the data get interpreted by the HTML?
From understanding the code above, events['value'] seems like a list of dictionaries. In the python code, you should be able to access it using
for inner_dict in events['value']:
print(inner_dict["subject"])
print(inner_dict["start"]["dateTime"])
print("*****")
The HTML code perhaps interprets it using Template Engine what does {% %} mean in html
"Simple" problem here which would already be solved in a view... But I didn't find a way to do it.
So here is the idea; I have Anonymous1, Anymous2... Anymous3 who received calls and communications times on phone numbers. I want to do for each of them a table like that :
Number | Calls | Communication time
2-xxx-x 1 01:00:00
3-xxx-x 23 00:30:00
total 24 01:30:00
Number of calls, communication time and total are all computed in the view, as it has to be dedicated to. I have a list of dictionnaries which contains all the numbers with the numbers of calls, the communication time and its owner. It is looking like that :
list = [{'number':2-xxx-x ,'owner':Anonymous1' ,'calls':1 ,'communication time':datetime object},...]
Why a list of dictionnaries ? Because I am using the regroup template tags as described in the documentation: https://docs.djangoproject.com/fr/1.9/ref/templates/builtins/#regroup
I also make a dictionnary which contains only the total number of calls, the total communication time and the owner; I am using it to compute the sum of each column. Here is it how it looks like :
second_dict = {'Anonymous1':{'calls':24,'communication_time':datetime object}}
To access them, I am using loops in my html code, which is where I have a problem. To create the table, I am regrouping the list of dictionnaries by their owner and performing loops and I am using the dictionnary to make th:
{% regroup list by owner as owner_list %}
{% for owner in owner_list %}
<table class="display" cellspacing="0" style="position: relative; left: -250px;">
<caption> {{owner.grouper}}</caption>
<thead>
<tr>
<th> {% trans "Number" %} </th>
<th> {% trans "Calls"%}</th>
<th> {% trans "Communication time" %}</th>
</tr>
</thead>
<tbody>
{% for item in owner.list%}
<tr>
<td>{{item.number}}</td>
<td>{{item.calls}}</td>
<td>{{item.communication_time}}</td>
</tr>
{% endfor %}
<tr>
{% with owner.list.0.owner as owner_name %}
<td><b>{% trans "Total" %}</b></td>
<td>{{second_dict.owner_name.calls}} </td>
<td> {{second_dict.owner_name.communication_time}} </td>
{% endwith %}
</tr>
</tbody>
</table>
{% endfor %}
As you can see with the code, I want to access the second dictionary values with owner as a key, following what is described here : https://docs.djangoproject.com/en/dev/ref/templates/api/
The problem is... this is not working at all ! I was thinking it was a str/unicode problem, but moving from one to the other when creating the different dictionnaries in the python views did not change anything.
Anyone got an hint on how to solve this ?
You cannot do lookup in a dictionary in template using a variable, dict in template would always treat what's after the dot as a string lookup like second_dict['owner_name']. You need to write a template filter to do it. Check django doc on how to write custom filter.
I have a table in SQLite3 and I need to take nearly all the columns in the table and output them to a web page. For other pages I've used flask, but that has always been a case of passing 3 or 4 single value variables into the template.
I'm guessing it's something like passing either the cursor or, more likely, the rows from the cursor.fetchall() call into the template then a for row in rows loop in the template?
It should be:
<table>
{% for item in items %}
<tr>
<td>{{column1}}</td>
<td>{{column2}}</td>
<td>{{column3}}</td>
<td>{{column4}}</td>
</tr>
{% endfor %}
</table>
You need to follow the below:
[HTML]:
<table>
{% for item in items %}
<tr>
<td>{{item[0]}}</td>
<td>{{item[1]}}</td>
<td>{{item[2]}}</td>
<td>{{item[3]}}</td>
</td>
{% endfor %}
[python code]:
cursor = db.execute('SELECT column1,column2,column3,column4 FROM tablename')
items = cursor.fetchall()
return render_template('print_items.html', items=items)
Pass your data as some sort of collection, whether that's something like a dictionary in the official tutorial, or something simpler, like a tuple or list.
Yes, in your template, you'll use a {% for item in collection %} -> {% endfor %} loop to render out all of the data from your database.
Example Python method:
#app.route('/print_items')
def print_items():
cursor = db.execute('SELECT column1,column2,column3,column4 FROM tablename')
return render_template('print_items.html', items=cursor.fetchall())
Example template section:
<table>
{% for item in items %}
<tr>
<td>column1</td>
<td>column2</td>
<td>column3</td>
<td>column4</td>
</td>
{% endfor %}
In Django, I am trying to generate a table. The first column is a timestamp. The next 5 columns need to be filled with my data.
However, my data table has 5 entries for each related timestamp. How can I put this data into my table with the correct headers?
The data entries are all seperate objects within Django..
The table has multiple rows with each their own timestamp, and their 5 corresponding data entries.
Is it possible to make this in the template layer, or view layer?
<table class="table table-hover">
<tr>
<th>date</th>
{% for sensor in sensors %}
<th>{{ sensor }}</th>
{% endfor %}
<th>Link</th>
</tr>
{% for stamp in stamps %}
<tr>
<td>{{ stamp.stamp|date:"D d N H:i" }}</td>
<td>Bekijk Detail</td>
</tr>
{% endfor %}
</table>
Instead of handling all this really complicated data in Django's template layer, this ought to be solved using the viewlayer.
First you create a new array, and seed it's table headers
table = ["header 1", "header 2"]
For each individual row you will add your data. The data I requested as a single datetime, followed by measurements. This equated to the following code
for x in range(0, len(timestamps)):
table.append(timestamp)
for datapoint in data:
table.append(datapoint)
You can then iterate through it in django's template layer.
<table>
{% for row in table %}
<tr>
{% for item in row %}
<td>{{item}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
You can add some custom if/else statements for headers and that kind of thing.