flask don't show table content while preview does - flask

code is:
def get_case():
...
table_step = zip(li_host,li_cmd)
return render_template('auto.html',table_step = table_step)
html is:
{% for row in table_step %}
<tr>
<td>{{ row[0]|safe }}</td>
<td>{{ row[1]|safe }}</td>
<td><button type="button" class="btn btn-default"> 编辑 </button></td>
</tr>
{% endfor %}
actual page shows no content:

Related

How to get data in the form of table in django using cloud firestore?

1 2 3
I'm trying to get data in the form of a table, but the data is not being fetched, I don't know how to get the record, I'm using cloud firestore to get the data.
In my code I have used {{buldings.building}} to get the record but this is not fetching the record from the firebase.
here is my table code for table
<div class="row">
<div class="col-lg-12 mb-4">
<!-- Simple Tables -->
<div class="card">
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<i class="fas fa-plus-circle "></i> Add Building
</div>
<div class="table-responsive">
<table class="table align-items-center table-flush" id="buildingList">
<thead class="thead-light">
<tr>
<th>BUILDING NAME</th>
<th>POSTAL CODE</th>
<th>CITY</th>
<th>STREET</th>
<th>HOUSE NO.</th>
<th>TOWN</th>
<th>ADDITIONAL INFO</th>
<th colspan="2">ACTION</th>
</tr>
</thead>
<tbody>
{% for building in buildings %}
<tr>
<td>{{ buildings.building }}</td>
<td>{{ buildings.postalCode }}</td>
<td>{{ buildings.city }}</td>
<td>{{ buildings.houseNo }}</td>
<td>{{ buildings.street }}</td>
<td>{{ buildings.town }}</td>
<td>{{ buildings.additionalInfo }}</td>
<td></i>Edit</p></td>
<td></i>Delete</p></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="card-footer"></div>
</div>
</div>
</div>
</div>
<!---Container Fluid-->
</div>
views.py file
def buildingMag(request):
context = {
'buildings': db.collection('Buildings').get()
}
return render(request,"EmployeeAdmin/buildingMag.html",context)
db.collection('Buildings').get() should return you a list of Firestore DocumentSnapshot which is sent to template context via buildings key. Looping through the buildings will let you populate the template(table) with each item on the list. Something like below.
In your views construct the dict from DocumentSnapshot and return the dict
views.py
buildings = db.collection('Buildings').get()
context = {
'buildings': [building.to_dict() for building in buildings]
}
template.html
{% for building_obj in buildings %}
<tr>
<td>{{ building_obj.building }}</td>
<td>{{ building_obj.postalCode }}</td>
<td>{{ building_obj.city }}</td>
...
...
</tr>
{% endfor %}

how to display retrieved data from database using django

i have a django project that is connected to SQL Server i tried to retrieve data from the database.
if i try to display all the data it run correct and display all the values.
but if i try to display the data in an html table it doesn't display anything.
views.py
def connect(request):
conn = pyodbc.connect(
'Driver={ODBC Driver 17 for SQL Server};'
'Server=DESKTOP-LPD1575\\SQLEXPRESS;'
'Database=testDB;'
'UID=test;'
'PWD=test;'
)
cursor = conn.cursor()
c = cursor.execute('SELECT * FROM Artist')
return render (request,'connect.html',{"c":c})
connect.html
{% for row in c %}
{{ row }}
{% endfor %}
this code in the html template work and display the data.
but if i try to do as below it will not work
<table align = "center">
<tr align="center">
<th>ID</th>
<th>FolderNumber</th>
<th>Title</th>
</tr>
{% for row in c %}
<tr align="center">
<td>{{ row.id }}</td>
<td>{{ row.artistName }}</td>
<td>{{ row.activeFrom }}</td>
</tr>
{% endfor %}
</table>
anyone can help me?
Each row in your result is a list, not a dict. You would need to use indexes not keys:
{% for row in c %}
<tr align="center">
<td>{{ row.0 }}</td>
<td>{{ row.1 }}</td>
<td>{{ row.2 }}</td>
</tr>
{% endfor %}
or better
{% for row in c %}
<tr align="center">
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
However, really you should not be running SQL queries directly, but define a model for your table and access it that way.

Filter and count in a for loop

In Django (2.0) I'm looping through records to display them in a table, I want a summary count to be displayed at the end for each record, I have this working using count = Fraction.objects.all().filter(botany_id=13).count() and the displaying {{ count }} however this is setup for record 13, the next record on the row has a botany_id=14 So how do I do this dynamically, essentially botany_id=botany_id
#views.py
def allflotation(request):
botany = Botany.objects.all()
fraction = Fraction.objects.all()
count = Fraction.objects.all().filter(botany_id=13).count()
fractioncomposition = FractionComposition.objects.all()
# fractionmaterialspresent = FractionMaterialsPresent.objects.all()
return render(request, 'flotation/allflotation.html',
{
'botany':botany,
'fraction':fraction,
'count':count,
'fractioncomposition':fractioncomposition,
# 'fractionmaterialspresent':fractionmaterialspresent
})
##### EDIT
#html
...
<tbody>
{% for botany in botany.all %}
<tr>
<td>{{ botany.sample_id }}</td>
<td>{{ botany.area_easting }}.{{ botany.area_northing }}.{{ botany.context_number }}</td>
<td>{{ botany.botany_id }}</td>
<td>{{ botany.sample_number }}</td>
<td>{{ botany.entry_date }}</td>
<td>{{ botany.flotation_date }}</td>
<td>{{ botany.analyst }}</td>
<td>{{ botany.notes }}</td>
<td>
<div class="btn-group" role="group" aria-label="Basic example">
<a href="{% url 'addfraction' pk=botany.botany_id %}">
<span class="badge badge-primary">
Add Fraction
<br />
<span class="badge badge-light">
{{ count }}
</span>
</span>
</a>
...
changing {{ count }} to {{ botany }} gives me the botany_id
Using related manager.
views.py
botanies = Botany.objects.all()
template.html
{% for botany in botanies %}
{{botany.fraction_set.count}}
{% endfor %}
You'll need to optimize number of queries later.

How to generate PDF in Django Generic Views?

I have a template that is rendered by generic List View. I want to give a download link in front of each row of data in table. The download link will create a PDF file of respective rows data. Please tell me how to write a code for that?
Views.py
class BookingConfirmationListView(LoginRequiredMixin, generic.ListView):
model = container_booking
template_name = 'home/booking_confirmation_detail.html'
context_object_name = 'all_container'
def get_queryset(self):
return container_booking.objects.all()
Templates look like
<table class="table-striped table-hover table-bordered" width="100%">
<tr>
<th>Date</th>
<th>Source</th>
<th>Destination</th>
<th>Container</th>
<th>Commodity</th>
<th>Agreed Rate</th>
<th>Edit</th>
<th>Delete</th>
<th>Status</th>
</tr>
{% for item in all_container %}
<tr>
<td>{{ item.date }} </td>
<td>{{ item.place_of_reciept }} </td>
<td>{{ item.final_place_of_destination }} </td>
<td>{{ item.equipment_type }}{{ item.quantity }} </td>
<td>{{ item.commodity }} </td>
<td>{{ item.agreed_rate }} </td>
<td><a href="{% url 'home:cont_bk-update' item.id %}" ><i class="fas
fa-edit"></i></a></td>
<td><a href="{% url 'home:cont_bk-delete' item.id %}" ><i class="fas
fa-trash"></i></a></td>
<td>{{ item.approved }}</td>
</tr>
{% endfor %}
</table>
urls.py
url(r'^cont_bkdetail$', views.BookingConfirmationListView.as_view(),
name='cont_bk-detail'),
url(r'^cont_bk/(?P<pk>[0-9]+)/$',
views.BookingConfirmationUpdate.as_view(), name='cont_bk-update'),
url(r'^cont_bk/(?P<pk>[0-9]+)/delete/$',
views.BookingConfirmationDelete.as_view(), name='cont_bk-delete'),
I want that whenever I click download, the PDF file of that row is generated.

Django Template : Combine 2 loops over different lists

I'm wondering how I can combine these 2 querysets in my template in order to loop over them.
requests = Download.objects.values('pub__age_id').annotate(count=Count('pub__age_id'))
max_download_number = Download.objects.values('pub__age_id').annotate(max_usage=Max('usage'))
context = {'requests': requests, 'max_download_number': max_download_number}
In my template :
{% for item in requests %}
{% for element in max_download_number %}
<tr>
<td>{{ item.pub__age_id }}</td>
<td><span class="badge alert-info">{{ item.count }}</span></td>
<td>{{ element.max_usage }}</td>
<td>Something</td>
</tr>
{% endfor %}
{% endfor %}
It displays wrong loops :
Why don't you combine it in the view:
requests = Download.objects.values('pub__age_id').annotate(count=Count('pub__age_id')).annotate(max_usage=Max('usage'))
and then in the template:
<td>{{ item.pub__age_id }}</td>
<td><span class="badge alert-info">{{ item.count }}</span></td>
<td>{{ item.max_usage }}</td>