I have lots of code like this in my template:
<h2>Owners of old cars</h2>
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
{% for result in old_cars %}
<tr>
<td>{{result.name}}</td>
<td>{{result.age}}</td>
<td>{{result.address}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h2>Owners of Ford cars</h2>
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
{% for result in ford_cars %}
<tr>
<td>{{result.name}}</td>
<td>{{result.age}}</td>
<td>{{result.address}}</td>
</tr>
{% endfor %}
</tbody>
</table>
There will be lots more tables created like those above. As you can see there is lots of duplicate code. Is there anyway to do this so I don't repeat as much code each time a create a table? Thanks.
UPDATE
I'm thinking about creating a new object, called table, and adding a result and the corresponding h2 title to it. I can then create a list of these table objects, called tables, which I can pass to the template. The template can then iterate over them.
Surely what you want, ideally, is:
{% for car in cars %}
<h2>Owners of {{ car.manufacturer }}</h2>
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
{% for model in car.models %}
<tr>
<td>{{model.name}}</td>
<td>{{model.age}}</td>
<td>{{model.address}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
I'm not sure what ORM you're using, but in Django that wouldn't be too hard to construct; after all, all you pass in is a list of objects with subobjects and fields.
Again, I can only talk for a django perspective (construct from your own ORM), but you can either deliberately construct the dictionary with explicit requests for each type of manufacturer, or rather have a model for "sales" and a model for "manufacturer" and construct a many-to-many relationship between the two. Your query roughly looks like this, in django python:
result = []
manufacturers = Manufacturer.objects.all() # get all manuf
for m in manufacturer:
mf = dict()
mf["manufacturer"] = m.name
mf["models"] = m.model_set.all() # get all linked models
result.append(mf)
# pass result to template as cars
Does that make sense?
Use template for the header and footer of the table
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
Put this in a file named "table_header.html" and include it in your template.
The same for the footer!
Related
I am trying to create a page where it shows records from the database and shows a delete button next to them. One of the elements shown is the variable needed for the delete function my problem is that I do not know how to get and pass that variable for the route that deletes the record from the database.
routes.py
#app.route("/deletetraining",methods=['GET', 'POST'])
def deleteTraining():
return render_template('deleteTraining.html', trainings = getAllTrainings(), title = 'Delete')
#app.route("/delete")
def delete(tid):
deleteTrainingByID(tid)
return redirect(url_for('deleteTraining'))
deleteTraining.html
{% extends 'layout.html' %}
{% block content %}
<h1 class="text-muted">All Trainings</h1>
<table class="table" >
<thead>
<tr>
<th scope="col">TID</th>
<th scope="col">Workplan</th>
<th scope="col">Cost Centre Code</th>
<th scope="col">Objective</th>
<th scope="col">Name</th>
<th scope="col">Location</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Hours</th>
<th scope="col">Days</th>
<th scope="col">Gender Hours</th>
<th scope="col">For MEL</th>
</tr>
</thead>
<tbody>
{% for training in trainings %}
<tr>
{% for element in training %}
<td>{{element}}</td>
{% endfor %}
<td>
<a class="btn btn-danger" type="button" href="/delete">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
In the template you must pass the id in the delete training button
<a class="btn btn-danger" type="button" href="/delete/{{training.id}}">Delete</a>
And in your #app.route you must add
#app.route("/delete/<tid>")
so I have some data being returned via API which I am passing into my template I am having an issue though in displaying the data as currently it is showing each char per row, I am trying to make it so in the table I have the coin name in a row with the value in the next col and so on.
Data being returned-
{"error":[],"result":{"ZGBP":"30622.0790","DASH":"0.5104491200","ADA":"2473.80445621","ZUSD":"67787.8285","KSM":"24.7142610000","CHZ":"13773.0349000000","XXLM":"6926.27220000","KNC":"0.0000000000","MATIC":"1838.9295772000","ZRX":"0.0000000000","BAL":"0.0000000000","XXDG":"17006.92601155","LINK":"144.2407000000","USDT":"60000.00000000","TRX":"923.80015900","COMP":"0.0000034600","ENJ":"257.6815000000","DOT":"0.0000000000","XLTC":"11.4923900000","SC":"0.0000000200","XZEC":"0.0000073100","SOL":"1133.3543869800","SUSHI":"172.4585500000","XXRP":"0.00000000","XETH":"14.5877343640","AAVE":"83.6218990800","ATOM":"151.26763831","XXBT":"0.0000012880","ALGO":"32063.69514500","OCEAN":"652.6077000000"}}
My template-
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Amount</th>
</tr>
</thead>
<tbody>
{% for v in api_reply %}
<tr>
<td>{{ v }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Probably you get the string object instead of json, but if it is a json...
In template use this:
<tbody>
{% for k,v in api_reply['result'].items() %}
<tr>
<td>{{ k }}</td>
<td>{{ v }}</td>
</tr>
{% endfor %}
</tbody>```
I have a page that displays rows from a db table. I added a button to each row that is designed to delete the row upon its click as well as redirect toward a deleted confirmation page. Everything seems to work, unless that nothing gets deleted in the database table.
Here is what my views.py look like:
def ProductsView(request):
queryset = Products.objects.all()
products_list = list(queryset)
request.session['context'] = products_list
#table = ProductsTable(queryset)
#context = {'table':table}
return render(request, 'inventory.html', {'products_list':products_list})
def DeleteProduct(request, Id):
Products.objects.filter(ProductId=Id).delete()
return render(request,'delete_confirmation.html')
and here is what the html page look like, including the code for the delete button
<table class="table text-center table-bordered table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">NAME</th>
<th scope="col">CATEGORY</th>
<th scope="col">TAG NUMBER</th>
<th scope="col">STOCK ON HAND</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody style="background-color: white">
{% for Products in products_list %}
<tr>
<td>{{ Products.ProductId }}</td>
<td>{{ Products.ProductName }}</td>
<td>{{ Products.ProductCategory }}</td>
<td>{{ Products.ProductTagNumber }}</td>
<td>{{ Products.StockOnHand }}</td>
<td>
<a class="btn btn-primary" href="{% url 'delete_confirmation' Id=Products.ProductId %}" style="color: white">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
here is what urls.py looks like:
path('inventory.html', ProductsView, name="inventory"),
path('delete_confirmation/<int:Id>', DeleteProduct, name="delete_confirmation"),
and when I print Id in console inside of the ``DeleteProduct``` view, it ouputs the correct Id value
I can't find out what I am doing wrong, I have been at it all day and need a pair of fresh eyes to look at it! Does anyone has a clue on what is wrong here?
I've a program which will read data from text file.
There are 3 types of data that I need to read from text file.
visual id
time
tname_pb
Problem
I have 3 columns and need to separate output into different columns.
But, visual id is working perfectly, but time and tname_pb are combined together.
Question
How can I separate time and tname_pb into two different column.
Current views.py based on the picture
with open(path) as input_data:
for line in input_data:
if line.startswith('2_visualid_'):
visual_key = line.lstrip('2_visualid_').rstrip()
data.update({visual_key: []})
if 'time' in tokens:
if search_time in line and visual_key in data:
data[visual_key].append(next(input_data).lstrip('2_mrslt_').rstrip())
if 'tname_pb' in tokens:
if tname_pb in line and visual_key in data:
data[visual_key].append(next(input_data).lstrip('2_mrslt_').rstrip())
Current template based on the picture
<div class="input-group" align="center" style=" left:10px; top:-110px; width:99%">
<table class="table table-bordered">
<thead class="success" >
<tr>
<th class="success">
<b>Visual ID</b>
</th>
<th class="success">
<b>Time Delay Index</b>
</th>
<th class="success">
<b>Tname PB</b>
</th>
</tr>
{% for key, values in output.items %}
<tr class="">
<td rowspan={{ values|length|add:1 }}><b>{{ key }}</b></td>
{% for value in values %}
<tr class="">
<td ><b>{{value}}</b></td>
</tr>
{% endfor %}
</tr>
{% endfor %}
</thead>
</table>
</div>
NOTE
I have tried few examples from Google, but I get errors with them.
I will post them here if they help clarify my question. I left them out because I don't want my question to be confusing.
You have a new table row going on here for each value.
{% for value in values %}
<tr class="">
<td><b>{{value}}</b></td>
</tr>
{% endfor %}
Instead of this, you need to make a new table cell for each value.
<table class="table table-bordered">
<thead class="success">
<tr>
<th class="success">
Visual ID
</th>
<th class="success">
Time Delay Index
</th>
<th class="success">
Tname PB
</th>
</tr>
</thead>
<tbody>
{% for key, values in output.items %}
<tr class="">
<td><b>{{ key }}</b></td>
{% for value in values %}
<td><b>{{ value }}</b></td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
I was hoping to have alternating color for my table. However, all rows become grey after I applied the table-striped class. I tried to load both v3 and v4 boostrap css files. And it still didn't work.
html
<table id="maxDiversificationTable" class="investmentTable table table-striped table-bordered table-hover table-fit" style="margin-top:-55%" >
<thead>
<tr style="color:#337AC7" >
<th >Tickers</th>
<th >Current Weight</th>
<th >New Weight</th>
<th >Conviction</th>
</tr>
</thead>
{% for tableData in dataSet %}
<tbody>
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
</tbody>
{% endfor %}
</table>
My guess is that your <tbody> which is also inside your for loop. So, your table is rendered like this:
<tbody>
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
</tbody>
<tbody>
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
</tbody>
Which is not what you want. What you want is the following:
<tbody>
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
</tbody>
So, try taking tbody out of the for loop and see if it works:
<tbody>
{% for tableData in dataSet %}
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
{% endfor %}
</tbody>
Hope it helps!
The table-striped class is defined in Bootstrap 4's SCSS as follows:
.table-striped {
tbody tr:nth-of-type(odd) {
background-color: $table-bg-accent;
}
}
So, in essence, the $table-bg-accent color will be applied to every odd row (tr) in every table body (tbody) element. Since you're creating a new table body for every row, every row will have the accent color applied.
To fix, don't create a new tbody for every row:
<thead>
<tr style="color:#337AC7">
<th>Tickers</th>
<th>Current Weight</th>
<th>New Weight</th>
<th>Conviction</th>
</tr>
</thead>
<tbody>
{% for tableData in dataSet %}
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
{% endfor %}
</tbody>