Update specific cell via python in html - python

im making a website that displays an object from the database with associated items(foreginKey). As of now i list the components in a table and it works fine. Also works with refresh to update the values. But i want only the cell with the dynamic value to update without calling the refresh to reload the whole page to stop flickering images etc.
Havent really tried much as im not so good at this, tho i figured that i need som "id" on the to make it targetable perhaps? Also i think i need to put the .value outside the initial for loop and maybe loop it seperatly inside SetInterval function. But how do I do that?
I am using Django 2.1.4 / Python 3.7.1 / Win10 / MSSQL running on localhost at the moment.
Also my first question here so hope the formatting is good.
<h2>{{ object.name }}</h2>
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th>Description</th>
<th>Name</th>
<th>Fabrikat</th>
<th>Type</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for komponent in object.komponent_set.all %}
<tr>
<td>{{ komponent.beskrivning }}</td>
<td>{{ komponent.namn }}</td>
<td>{{ komponent.fabrikat }}</td>
<td>{{ komponent.typ }}</td>
<td>{{ komponent.value }}</td>
</tr>
{% endfor %}
<script>
var myVar = setInterval(valueRefresh, 5000);
function valueRefresh() {
{% for komponent in aggregat.komponent_set.all %};
<td>{{ komponent.value }}</td>;
{% endfor %};
}
</script>
</tbody>
</table>
</div>
Sorry if this is rookie hour, but i dont know how to go about this.

Related

Generating new HTML rows for each Document from firestore in python Django

I have a collection of documents in a Firestore database. I want to create a web form to display all the documents and their fields. I started by streaming all the documents using:
docs = db.collection(u'users').stream()
and then appending all the docs IDs to a list and pass it to the HTML file as follows
def index(request):
myIDs =[]
db = firestore.Client()
docs = db.collection(u'users').stream()
for doc in docs:
myIDs.append(f'{doc.id}')
return render(request, 'index.html', {
"weight":"abc",
"firstRow":myIDs[0],
"secondRow":myIDs[1],
"thirdRow":myIDs[2],
"fourthRow":myIDs[3],
"fifthRow":myIDs[4],
"sixthRow":myIDs[5],
"seventhRow":myIDs[6],
})
After that, I created a very basic HTML code just to display the document IDs as follows:
<html>
<body>
<table border="1" cellpadding = "5" cellspacing="5">
<tr>
<td>{{ firstRow }}</td>
<tr>
<td>{{ secondRow }}</td>
</tr>
<tr>
<td>{{ thirdRow }}</td>
</tr>
<tr>
<td>{{ fourthRow }}</td>
</tr>
<tr>
<td>{{ fifthRow }}</td>
</tr>
<tr>
<td>{{ sixthRow }}</td>
</tr>
<tr>
<td>{{ seventhRow }}</td>
</tr>
</tr>
</table>
</body>
</html>
Till this point I am just doing very basic stuff. But I am new to Django and Python for the Web, so my question is: how can I make it so that the HTML rows become adaptive based on the number of documents?
For example, if I have 20 documents, I want to make it so that it will generate the 20 rows and pass each of the document IDs to one row, and so on.
This is fairly simple in Django
You can pass the list MyIDs as a context variable and let your template do the heavy lifting
return render(request, 'index.html', {
"weight":"abc",
"myIDs": myIDs
})
Then in your template
<table border="1" cellpadding = "5" cellspacing="5">
{% for id in myIDs %}
<tr>
<td>{{ id }}</td>
</tr>
{% endfor %}
</table>
(I suspect you can make this even simpler by passing docs as a context variable, which would let you do
{% for doc in docs %}
<tr>
<td>{{ doc.id }}</td>
<td>{{ doc.title }}</td>
</tr>
{% endfor %}
</table>
but I'm not 100% familiar with the output of stream() )

Issue with passing python function to jinja

I am working on a Flask web app and I am at a point where I want the admin of the site to be able to approve/deny users. The issue I am facing is that I am trying to call Python functions in html/jinja template. Here is a look. demo
In the user_approval.html file
``
<table id="user-approval" class="table table-striped table-bordered">
<thead>
<tr>
<th>Full Name:</th>
<th>Address:</th>
<th>DOB:</th>
<th>Email:</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{% for user in users.items %}
<tr>
<td>{{ user.a_name }}</td>
<td>{{ user.a_address }}</td>
<td>{{ user.a_dob }}</td>
<td>{{ user.a_email }}</td>
<td><button id="approve-user-btn" onclick="{{ user_approve }}">approve</button></td>
<td><button id="deny-user-btn" onclick="{{ user_deny }}">deny</button></td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th>Full Name:</th>
<th>Address:</th>
<th>DOB:</th>
<th>Email:</th>
<th></th>
<th></th>
</tr>
</tfoot>
</table>
``
I would need to pass the user_id as an argument. I tried to do something like this
<button id="approve-user-btn" onclick="{{ user_approve(user.id) }}">approve</button>
Which calls the functions before admin clicks.
This is not going to work, the onclick event can be used to run Javascript, but in this case you should rather create a route in Jinja to run Pyton code. You need to add a <form> in your HTML too, and in your form you set the target accordingly (using the function url_for). Change the button like this too, so it behaves like a submit element:
<button id="approve-user-btn" type="submit"...
or
<input id="approve-user-btn" type="submit"...
And probably add a hidden field for the user ID as well. This value will be included in the form (POST) request.

Show only "Reorder" item

I am using Django and in the front end, I am trying to show only "Reorder" Parts on the table. To define the status of Available and Reorder, I am normally using the if-else method to track. I was thinking about filtering but I have no such Status field in my DB. Anyone can give me any idea how to do that? Here is what I've done for now
HTML
<tr>
<th class="serial">#</th>
<th>Part No</th>
<th>Part Name</th>
<th>Quantity</th>
<th>Status</th>
<th>Date</th>
<th>Action</th>
</tr>
{% if parts %}
{% for part in parts %}
<tr>
<td class="serial">{{ forloop.counter }}</td>
<td>{{ part.partno }}</td>
<td>{{ part.partname }}</td>
<td>{{ part.quan }}</td>
<td>
{% if part.quan <= part.limit %}
<p style="color: #FF0000">
Reorder
</p>
{% elif part.quan > part.limit %}
<p style="color:#008000">
Available
</p>
{% endif %}
</td>
<td>{{ part.created_date }}</td>
</tr>
Part Table
You can reference an value from the instance in a filter, this queryset will return your parts that need reorder
from django.db.models import F
parts = Part.objects.filter(quan__lte=F('limit'))

Need help in Django Templates(using forloops)

I have 3 models namely Uses, Customer and Services. I have a query whose result is stored in the variable count.
This is the query
counts = Services.objects.filter(uses__customer=customer).annotate(times_used=Count('uses'))
Each count[x].times_used has a variable,x ranges from 0 to infinity.
I have used a for loop in my html to get all the Services which I have passed through context in views.
Now I need to get unique value of count for each service. How can I do this.
Here is my html code
<table class='table table-bordered' id='dataTable' width='100%' cellspacing='0'>
<thead class='bg-primary'>
<tr class='text-white'>
<th>Service-Id</th>
<th>Service-name</th>
<th>Count</th>
<th>Price</th>
</tr>
</thead>
<tbody class="text-dark">
<tr>{% for service in services %}
<td>{{ service.id }}</td>
<td>{{ service.service_name }}</td>
<td>#code for printing count[x],times_used#</td>
<td>{{ service.price }}</td>
</tr>{% endfor %}
</tbody>
</table>
{% for elem_1 in results_1 %}
{{elem_1}}
{% for elem_2 in results_2|index_filter:forloop.counter0 %}
{{elem_2}}
{% endfor %}
{% endfor %}
You can use that structure in your template, anywhere.
You use a builtin filter "index_filter".
Basically, when myvar is piped with | to index_filter:forloop.counter0, then the function will call the element in results_2 with index = forloop.counter0. This later counter being the one of the parent for loop.

How to Display Python Flask DB entires in Jinja 2 html table

I currently have a DB whose table I want to review inside of my web app via a specific route.
I want to view the Jinga2 output of the entires in the DB in a easy to read table via the app.
What I currently have works but it shows all the entries in one line instead of a table format like an excel spreadsheet.
My code is below:
{% for row in rows %} <tr>
<td>{{ row.id }}</a></td>
<td>{{ row.question }}</td>
<td bgcolor="{{ row.stat_colour }}">{{ row.unit_status }}</td> </tr> >
{% endfor %}
For anyone having similar issues, I was able to get it to work by using the code block below for my html:
<html !DOCTYPE>
<head>
<title>SQLite 3 DB Questions Table</title>
</head>
<body>
<table>
{% for row in rows %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.question }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
Put the opening tr before the start of the loop
<tr> {% for row in rows %}

Categories

Resources