I'm trying to parse the following data structure in my HTML.
{'GROUPS': {'Group1': [{'key1':'value1','key2':'value2'}, {'key1':'value3', 'key2':'value4'}], 'Group2': [{'key1':'value5','key2':'value6'}, {'key1':'value7', 'key2':'value8'}]}}
The parsing code that I have is as follows:
<tbody>
{% for group,data in data|get_value:"GROUPS" %}
<tr>
<td>{{ group }}</td>
{% for v in data.items %}
<tr>
<td>{{ v|get_value:"key1" }}</td>
<td>{{ v|get_value:"key2" }}</td>
</tr>
{% endfor %}
</tr>
{% endfor %}
</tbody>
get_value is the custom filter that I've written which basically takes the key and the data structure, and returns the value back.
But this isn't working. Can anyone help me figure out why? Thanks!
Firstly, for constant keys, you don't need a custom filter, this will work just fine:
{{ v.key1 }}
That said, data['GROUPS'] is a dict, and you want to iterate over its items, like you did with data.
data is a list though and doesn't need that:
<tbody>
{% for group, data in data.GROUPS.items %}
<tr>
<td>{{ group }}</td>
{% for v in data %}
<tr>
<td>{{ v.key1 }}</td>
<td>{{ v.key2 }}</td>
</tr>
{% endfor %}
</tr>
{% endfor %}
</tbody>
Related
I am trying to get url parameters like this:
<tbody>
{% for object in objects %}
<tr>
<td>{{ object.id }}</td>
<td>{{ object.user }}</td>
<td>{{ object.url }}</td>
<td>{{ object.minimum }}</td>
<td>{{ object.maximum }}</td>
<td>{{ object.requests }}</td>
<td>{{ object.stay }}</td>
<td class="text-Centre">
Run
</td>
</tr>
{% endfor %}
</tbody>
If I put my URL like this, it works since the view exists in view.py:
{% url 'trafficapp:generate_traffic' %}
Now, I am trying to take URL parameters with it, but I am unable to make how can I pass data from this button, and what should I put in my URL pattern for this. Kindly help, I am new to Django and still figuring out how it works.
you can add parameters after {% url %}
Run
I have a flask server that is sending a dictionary to the frontend. I'm trying to sort the contents of the dictionary in the UI.
Here is how the dictionary is structured.
{
'Var1':
{
'weight':1,
'other_stuff':'foo'
},
'Var2':
{
'weight':0.5,
'other_stuff':'bar'
},
'Var3':
{
'weight':2,
'other_stuff':'baz'
},
...
}
The relevant snippet from the UI that isn't what I need to sort looks something like this. Note this works just fine, no errors, but it isn't sorting the data.
{% for key,var in d %}
<tr>
<td>{{ key }}</td>
{% if var['weight']>0.01 or var['weight']<-0.01 %}
<td>{{ var['weight'] }}</td>
{% else %}
<td>0.01</td>
{% endif %}
</tr>
{% endfor %}
What I want to do is to sort this dictionary when displaying it, obviously dictionaries have no ordering in python, by the weight. Desired output would be the data being displayed like this:
<tr>
<td>Var3</td>
<td>2</td>
</tr>
<tr>
<td>Var1</td>
<td>1</td>
</tr>
<tr>
<td>Var2</td>
<td>0.5</td>
</tr>
I've tried to use do_dictsort filter, but it gave me an error because of how the data was organized. How do I sort this in the UI?
why don't you send the sorted dict to the front end..now you just have to loop.
here's how
data = sorted(d.items(), key=lambda x: x[1]['weight'], reverse=True)
{% for key,var in d %}
<table>
<tr>
<td>{{ key }}</td>
<td>{{ var['weight'] }}</td>
</tr>
</table>
{% endfor %}
In my django template code, I've written code for table in nested for loops. By using with statement, I want to put serial number. But it is not working. Have a look at my code in html file:
<tbody>
{% for a_testcases in testcases %}
{% for a_test in a_testcases %}
{% with x=forloop.parentcounter.counter %}
<tr>
<td id="sno_{{a_test.id}}">{{ forloop.counter|add:x }}</td>
<td id="name_{{a_test.id}}">{{ a_test.name }}</td>
<td id="cputime_{{a_test.id}}">{{ a_test.safe_exec.cpu_time }}</td>
<td id="clocktime_{{a_test.id}}">{{ a_test.safe_exec.clock_time }}</td>
<td id="memorylimit_{{a_test.id}}">{{ a_test.safe_exec.memory }}</td>
<td id="stacksize_{{a_test.id}}">{{ a_test.safe_exec.stack_size }}</td>
<td id="child_{{a_test.id}}">{{ a_test.safe_exec.child_processes }}</td>
<td id="openfiles_{{a_test.id}}">{{ a_test.safe_exec.open_files }}</td>
<td id="maxfilesize_{{a_test.id}}">{{ a_test.safe_exec.file_size }}</td>
</tr>
{% endwith %}
{% endfor %}
{% endfor %}
</body>
It is showing nothing in place of serial number.How to fix it?
I'm trying my best not to repeat myself in my code but I'm encountering a problem looping through a dictionary by key in my template.
I have two dicts:
exampledict={'firstkey':firstval, 'secondkey':secondval}
keys=['firstkey', 'secondkey']
keydict={'keys':keys}
In my template I want to loop over the exampledict using the keydict:
<tr>
{% for val in keydict %}
<td>{{ exampledict.val }}</td>
{% endfor %}
</tr>
I've noticed this kind of combination of variables doesn't work at all, I tried by using:
{'firstkey':'firstkey'}
And sending that through to the template and later trying
{{ exampledict.firstkey }}
Is there a better way to accomplish what I'm trying to do here?
Edit 1:
Manually going through each key as:
<td> {{ exampledict.firstkey }} </td> <td> {{ exampledict.secondkey }} </td>
Where firstkey and secondkey is the actual dictkey for exampledict works, although it makes for a lot of repetition.
Edit 2:
views.py
def tabletest(request):
exampledict={'firstkey':'firstval', 'secondkey': 'secondval'}
keydict={
'keys':['firstkey', 'secondkey']
}
return render(request, 'MinaFakturor/tabletest.html', {'exampledict':exampledict, 'keydict':keydict})
template
<table>
<thead>
<tr>
{% for val in keydict.keys %}
<th>{{ val }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for val in keydict.keys %}
<td>{{ exampledict.val }}</td>
{% endfor %}
</tr>
<tr>
<td>{{ exampledict.firstkey }}</td>
</tr>
</tbody>
</table>
Which produces this result:
If I remove the exampledict.firstkey term, nothing is produced in the table body.
I am trying to create HTML tables from data stored in a table. My data is read from a table and converted into a dict of lists, e.g.:
x = {'date':[u'2012-06-28', u'2012-06-29', u'2012-06-30'], 'users': [405, 368, 119]}
My goal is to create an HTML table with the following structure for an arbitrary list length:
<table>
<thead>
<th>Date</th>
<th>Users</th>
</thead>
<tbody>
<tr>
<td>2012-06-28</td>
<td>405</td>
</tr>
<tr>
<td>2012-06-29</td>
<td>368</td>
</tr>
<tr>
<td>2012-06-30</td>
<td>119</td>
</tr>
</tbody>
</table>
I have tried doing this two incorrect ways in my Flask template:
<tbody>
{% for line in x %}
<tr>
<td>{{ x.date|tojson|safe }}</td>
<td>{{ x.users }}</td>
</tr>
{% endfor %}
</tbody>
Which prints the entire list into each column.
And:
{% for date in x.date %}
<tr><td>{{ date|tojson|safe }}</td></tr>
{% endfor %}
{% for users in x.users %}
<tr><td>{{ users }}</td></tr>
{% endfor %}
Which simply prints everything into the first column.
These experiments and many other dead ends lead me to believe that there is no simple way to build the table as I would like given my current data structure.
Given this, I have two questions:
1) How would I go about building the table using my current data structure?
2) What is the standard or ideal way to structure data for this use case?
Thanks in advance.
Like you said, you could either change your data structure, or change your template code. Here is one way to keep the current structure:
{% for row_index in range(x['date']|count) %}
<tr>
<td>{{ x[row_index]['date']|tojson|safe }}</td>
<td>{{ x[row_index]['users'] }}</td>
</tr>
{% endfor %}
Or you could restructure your data in python:
x = zip(x['date'], x['users'])
And then use this template:
{% for row in x %}
<tr>
<td>{{ row[0]|tojson|safe }}</td>
<td>{{ row[1] }}</td>
</tr>
{% endfor %}
You can also structure the data so that the template does not depend on the order of the cells:
from itertools import izip
x = [dict(date=d, user=u) for d, u in izip(x['date'], x['users'])]
Then you can access your data like this:
{% for row in x %}
<tr>
<td>{{ row['date']|tojson|safe }}</td>
<td>{{ row['user'] }}</td>
</tr>
{% endfor %}
You might use Flask-Table or for something more complex even leverage Flask-Admin.
Yeah, you really want to use a list of dictionaries instead of a dictionary of lists, that works out better with Jinja2