Empty string when accessing dictionary value in Jinja2 - flask

Trying to add the value of item['val'] to the table results in empty space
item.num is a list of numbers that is shown as an integer on the table, where as item.val is a float number that needs to be shown as is on the table
app.py
#app.route('/')
def index():
items = Item.query.filter_by().all()
# item.num is a list of items
# item.val is a float value
tmp = {"item_num":item.num , "item_val":str(item.val)}
result[item.name] = tmp
return render_template("information.html", info=result)
information.html
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">item num</th>
<th scope="col">item val</th>
</tr>
</thead>
<tbody>
{% for item in info %}
<tr>
<td>{{ item }}</td>
<td>{{ item['num'] | length }}</td>
<td>{{ item['val'] }}</td>
</tr>
{%endfor%}
</tbody>
how do you add a value from the dictionary to the table

If I understand you correctly, you have a dict with a nested dict, like in this example. The name of an item represents the key value under which further data is stored. Since you don't mention your second table in your code, I can't refer to that one.
#app.route('/')
def index():
items = Item.query.all()
result = { item.name: {'num': item.num, 'val':str(item.val)} for item in items }
return render_template('information.html', info=result)
So you can iterate over all key-value pairs with the keyword items() and then query the data from the nested dict.
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">item num</th>
<th scope="col">item val</th>
</tr>
</thead>
<tbody>
{% for k,v in info.items() %}
<tr>
<td>{{ k }}</td>
<td>{{ v['num'] | length }}</td>
<td>{{ v['val'] }}</td>
</tr>
{%endfor%}
</tbody>

Related

Adding a new table row using HTMX

I am trying to add a new table row with Htmx. By clicking the "Add A New Row" button, a new row should be added to the table.
I can't get the row in the secondary_hx.html to be added to the table, instead it results as three forms next to each other without or .
Any help would be appreciated. Thank you in advance.
My code is like this:
main.html:
<table id="myTable">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
</thead>
<tbody id="Tbody">
<tr>
<td>
<button class="add_button"
type="submit"
hx-post="/add_row/"
hx-target="#Tbody"
hx-swap="beforeend"
>
Add A New Row
</button>
</td>
</tr>
</tbody>
</table>
partials/secondary_hx.html:
<tr hx-target="this"
hx-swap="outerHTML">
<td>{{ form.a }}</td>
<td>{{ form.b }}</td>
<td>{{ form.c }}</td>
</tr>

Jinja2 - Loop over list to build a table

I am quite new to Flask and I am having a hard time to understand why I am only getting a list of elements in my browser (single column), I would like to get 3 different columns and my data is correct:
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Mail</th>
<th>Delete</th>
</tr>
{% for n in customers %}
<tr>
<td>{{n['First Name']}} </td>
</tr>
<tr>
<td>{{n['Last Name']}}</td>
</tr>
<tr>
<td>{{ n['Phone']}}</td>
</tr>
<tr>
<td> Supprimer <td></td>
</tr>
{% endfor %}
</table>
What you are really looking for is something like this:
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Mail</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{% for n in customers %}
<tr>
<td>{{n['First Name']}} </td>
<td>{{n['Last Name']}}</td>
<td>{{ n['Phone']}}</td>
<td> Supprimer <td></td>
</tr>
{% endfor %}
</tbody>
</table>
tr Stands for Table Row. Check out this link to learn a bit more about tables

Is there any way to save and HTML table including forms without creating a model in Django?

I have created a table with django forms in it. The forms do get data from the database, from two different models. Since this form has 42 fields consisting of 7 days and 6 shifts, i would love to save it as the table it is rendered. Is there any way to do this?
forms.py
class EditSchedule(forms.Form):
def __init__(self,*args,**kwargs):
super(EditSchedule, self).__init__(*args,**kwargs)
for k in range(1,8):
for i in range(1,7):
self.fields["S"+str(i)+"D"+str(k)] = forms.ChoiceField(choices=get_my_choices(i,k))
self.fields["S"+str(i)+"D"+str(k)].widget.attrs.update({"class":"form-control select2 select2-hidden-accessible"})
html file
<div class="box">
<form method="POST" action="">{% csrf_token %}
<div class="box-body">
<div class="table-container table-responsive">
<table class="table table-bordered table-hover dataTable" role="grid" >
<thead>
<tr>
<th class = "shicht"><h3>Schicht</h3></th>
<th class = "montag"><h3>Montag</h3></th>
<th class = "dienstag"><h3>Dienstag</h3></th>
<th class = "mittwoch"><h3>Mittwoch</h3></th>
<th class = "donnerstag"><h3>Donnerstag</h3></th>
<th class = "freitag"><h3>Freitag</h3></th>
<th class = "samstag"><h3>Samstag</h3></th>
<th class = "sonntag"><h3>Sonntag</h3></th>
</tr>
</thead>
<tbody>
<tr class="even">
<td class="shicht">Schicht 1</td>
<td class="montag">{{ form.S1D1 }}</td>
<td class="dienstag">{{ form.S1D2 }}</td>
<td class = "Mittwoch">{{ form.S1D3 }}</td>
<td class = "donnerstag">{{ form.S1D4 }}</td>
<td class = "freitag">{{ form.S1D5 }}</td>
<td class ="samstag">{{ form.S1D6 }}</td>
<td class ="sonntag">{{ form.S1D7 }}</td>
</tr>
<tr class="odd">
<td class="shicht">Schicht 2</td>
<td class="montag">{{ form.S2D1 }}</td>
<td class="dienstag">{{ form.S2D2 }}</td>
<td class = "Mittwoch">{{ form.S2D3 }}</td>
<td class = "donnerstag">{{ form.S2D4 }}</td>
<td class = "freitag">{{ form.S2D5 }}</td>
<td class ="samstag">{{ form.S2D6 }}</td>
<td class ="sonntag">{{ form.S2D7 }}</td>
</tr>
<tr class="even">
<td class="shicht">Schicht 3</td>
<td class="montag">{{ form.S3D1 }}</td>
<td class="dienstag">{{ form.S3D2 }}</td>
<td class = "Mittwoch">{{ form.S3D3 }}</td>
<td class = "donnerstag">{{ form.S3D4 }}</td>
<td class = "freitag">{{ form.S3D5 }}</td>
<td class ="samstag">{{ form.S3D6 }}</td>
<td class ="sonntag">{{ form.S3D7 }}</td>
</tbody>
</table>
</div>
</div>
<input type="submit" value="Save">
You can use raw queries for this.
For capture the form filds you can use the "clean" methods in the base form class see forms validation

django trying to put a table in a view

in my views.py:
def show(request):
query_results=Emails.objects.all()
#from py_utils import open_py_shell;open_py_shell.open_py_shell()
context = { 'query_result' : query_results }
return render(request, 'show.html', context)
in show.html:
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
{% for item in query_result %}
<tr>
<td>Ohad</td>
<td>{{ item.email }}</td>
<td>{{ item.baseurl }}</td>
</tr>
{% endfor %}
</tbody>
</table>
an email object has email field and baseurl field but it seems that i cannot load it,
i checked and in query_result i have a list of Emails object just cannot put it in the table, any help?

Trying to generate rows in a table

here's what my models look like:
http://i.imgur.com/fFBqq.png
I'm trying to fill out a table full of disks, each has a serial and model number. I'm going to assume I'll do something like {% for disks in "something" %}, but I'm not quite sure what that something would be.
Here's what i was hoping for:
<table>
<thead>
<tr>
<th>Serial Number</th>
<th>Model Number</th>
</tr>
</thead>
<tbody>
{% for disks in "something" %}
<tr>
<td>{{ disk.serial }}</td>
<td>{{ disk.model }}</td>
</tr>
{% endfor %}
The template is only part of your issue. It's actually the less complicated aspect, as all you are doing is passing it a context (a dictionary) for it to access. The step before the template is the view that is organizing the data. Lets start with that...
View
The function (view) that gathers the data needs to build a context containing your "disk" objects, assumably a result of a database model query. For simplicity, lets just say you did this:
disks = Disk.objects.all()
With your disks queryset, you can now deliver that to your template in the context.
context = {"disks": disks}
return render_to_response('my_template.html', context)
The context will now be passed to your template.
Template
Simply refer to the objects in your context:
{% for disk in disks %}
<tr>
<td>{{ disk.serial }}</td>
<td>{{ disk.model }}</td>
</tr>
{% endfor %}
#jdi is right, but since this is something very commonly done in web development - there is a generic view for it.
In your urls.py:
from django.conf.urls import patterns, url, include
from django.views.generic import ListView
from myapp.models import Disk
urlpatterns = patterns('',
(r'^disk_list/$', ListView.as_view(
model=Disk,
template_name='disk_list.html'
)),
)
Create a file called disk_list.html, that is any directory listed in TEMPLATE_DIRS, and in it add this:
<table>
<thead>
<tr>
<th>Serial Number</th>
<th>Model Number</th>
</tr>
</thead>
<tbody>
{% for disk in object_list %}
<tr>
<td>{{ disk.serial }}</td>
<td>{{ disk.model }}</td>
</tr>
{% endfor %}
Finally, navigate to http://localhost:8000/disk_list/