In my Flask application I use Bootstrap 5 and datatables.
The basic datatable is ok.
I want to add buttons and clickEvent and that's where it goes wrong.
Here's my code
{% extends "base.html" %}
{% block title %}Modify stek{% endblock %}
{% block content %}
<br/>
<h1>Pagina voor het wijzigen van een stek!</h1>
<table id="stekTable" class="table table-striped table-hover">
<thead style="color: #F8F8FF; background-color: #191919 ">
<tr>
<th>Stek</th>
<th>Water</th>
<th>Beschrijving</th>
<th>Type</th>
<th>Plaats</th>
</tr>
</thead>
<tbody>
{% for stek, user, stektype in stekken %}
<tr>
<td>{{ user.email }}</td>
<td>{{ stek.name }}</td>
<td>{{ stek.place }}</td>
<td>{{ stek.latitude }}</td>
<td>{{ stektype.stektype }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<br/>
{% endblock %}
{% block scripts %}
<script>
$(document).ready(function () {
$('#stekTable').DataTable(
{
//retrieve: true,
dom: 'Bfrtip',
buttons: ['copy', 'excel'],
});
});
</script>
<script>
var table = $('#stekTable').DataTable();
$('#stekTable tbody').on( 'click', 'tr', function () {
alert( table.row(this).data() );
});
</script>
{% endblock %}
Without the retreive statement: I have working buttons, but no clickEvent
With the retreive statement: I have no buttons at all and a working clickEvent
What needs to be done to have them both ?
I fixed the issue by moving the “clikEvent function” into the scope of the $(document).ready(function).
Related
I started working with Django earlier this week and watched a few tutorials and thought I would try out switching over a site I made in PHP to something a little more current. I ran into some issues over the weekend and managed to get help with some of them, but one issue remains.
I already have an existing database with some information it that I want to retrieve to display on a website. The information is modified through the database so Django doesn't have to deal with any of that - just displaying results to a bootstrapped template file.
So, in essence Django would have 2 models - one for each relevant database table.
The first model relates to products
class Bikes(models.Model):
bikempn = models.CharField(primary_key=True, max_length=50)
bikecategory = models.CharField(max_length=50)
bikeyear = models.CharField(max_length=4)
bikebrand = models.CharField(max_length=50)
bikedesc = models.CharField(max_length=255)
bikesize = models.CharField(max_length=50)
bikecolour = models.CharField(max_length=255)
bikeurl = models.CharField(max_length=255)
class Meta:
managed = False
db_table = 'bikes'
The second model relates to their expected arrival dates. This model would represent a database view which performs some logic comparing product that is on its way to the number of matching items that are on reserve.
The model is:
class Eta(models.Model):
bikempn = models.ForeignKey(Bikes, on_delete=models.CASCADE, primary_key = True, db_column='bikempn', related_name='etas')
eta = models.DateField()
class Meta:
managed = False
db_table = 'bike_eta'
The idea of this website is essentially to let people know if a product they are interested in will be arriving any time soon.
I have been trying to come up with a query that will display all of the linked information, but everything I find online hasn't quite worked. I have received some help along the way but have hit the wall again and frankly feel foolish having not figured this out yet.
So, currently I have a query that is:
kidsquery = Bikes.objects.filter(bikecategory='kids').select_related('etas')
This filters out only bikes categorized as kids bikes and should join the two models together.
My relevant template to display this information is:
{% extends 'base.html' %}
{% block content %}
{% if kidsquery %}
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered table-sm">
<thead class="table-dark">
<tr>
<th scope="col">Year</th>
<th scope="col">Brand</th>
<th scope="col">Model</th>
<th scope="col">Colour</th>
<th scope="col">Size</th>
<th scope="col">Part#</th>
<th scope="col">ETA</th>
</tr>
</thead>
{% for info in kidsquery %}
<tr>
<td>{{ info.bikeyear }}</td>
<td>{{ info.bikebrand }}</td>
{% if info.bikeurl %}
<td>{{ info.bikedesc }}</td>
{% else %}
<td>{{ info.bikedesc }}</td>
{% endif %}
<td>{{ info.bikecolour }}</td>
<td>{{ info.bikesize }}</td>
<td>{{ info.bikempn }}</td>
{% for arrival in info.etas.all %}
{% if arrival is null %}
<td>Contact Us</td>
{% else %}
<td>{{ arrival|date:"F Y" }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
{% endif %}
{% endblock %}
Does anyone know why I cannot get anything to display in the final column (where it should display the eta date values (or failing that, the "Contact Us" result)?
The proper solution to pulling the information out in the template:
{% extends 'base.html' %}
{% block content %}
{% if kidsquery %}
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered table-sm">
<thead class="table-dark">
<tr>
<th scope="col">Year</th>
<th scope="col">Brand</th>
<th scope="col">Model</th>
<th scope="col">Colour</th>
<th scope="col">Size</th>
<th scope="col">Part#</th>
<th scope="col">ETA</th>
</tr>
</thead>
{% for info in kidsquery %}
<tr>
<td>{{ info.bikeyear }}</td>
<td>{{ info.bikebrand }}</td>
{% if info.bikeurl %}
<td>{{ info.bikedesc }}</td>
{% else %}
<td>{{ info.bikedesc }}</td>
{% endif %}
<td>{{ info.bikecolour }}</td>
<td>{{ info.bikesize }}</td>
<td>{{ info.bikempn }}</td>
{% for arrival in info.etas.all %}
{% if arrival.eta %}
<td>{{ arrival.eta|date:"F Y" }} </td>
{% else %}
<td>Contact Us</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
{% endif %}
{% endblock %}
Swapped around the checks, and compensated for the lack of a date.
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 %}
I got 2 queryset list expired_item and queryset in Django ListView, but I don't know when item is expired(queryset is empty), how to display another list expired_item on frond end, no matter what I changed in abc.html, expired_item won't dispaly, I pasted my code as below:
class ABCListView(ListView):
model = ABC
ordering = ('name', 'skill_course')
context_object_name = 'abcs'
template_name = ''
def get_queryset(self, **kwargs):
# Omitted
......
......
# Omitted
expired_item = list(ABC.objects.filter(pk__in=aa).exclude(pk__in=z))
queryset = Permit.objects.filter(pk__in=z)
return queryset
And my html file of abc.html as below:
{% extends 'base.html' %}
{% block content %}
<nav aria-label="breadcrumb">
</nav>
<h2 class="mb-3">My Items list</h2>
<div class="card">
<table class="table mb-0">
<thead>
<tr>
<th>Name</th>
<th>Department</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
{% for a in abcs %}
<tr>
<td class="align-middle">{{ a.name }}</td>
<td class="align-middle">{{ a.department.get_html_badge }}</td>
<td class="align-middle badge badge-pill badge-danger">{{ a.status }}</td>
</tr>
{% empty %}
{% endfor %}
</tbody>
</table>
</div>
<h2 class="mb-3">My Expired Items list</h2>
<div class="card">
<table class="table mb-0">
<thead>
<tr>
<th>Name</th>
<th>Department</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
{% for b in expired_item %}
<tr>
<td class="align-middle">{{ b.name }}</td>
<td class="align-middle">{{ b.department.get_html_badge }}</td>
<td class="align-middle badge badge-pill badge-danger">{{ a.status }}</td>
</tr>
{% empty %}
{% endfor %}
</tbody>
</table>
</div>
<div class="card-footer">
{% endblock %}
Thanks so much for any advice!
I would suggest use a normal django view. This Generic ListView is just created for the use of one list. Just pass both querysets in your context and render your template with that.
You could also use get_context_data() but this would be more or less hacky and not the qay I would recommend.
I have some model
from django.db import models
class Model1(models.Model):
title = models.CharField(max_length=300)
boolean1 = models.BooleanField(default=False)
boolean2 = models.BooleanField(default=False)
and am currently displaying all database entries in a template via the following bootstrap table:
<div class = 'table-responsive'>
<table class='table table-striped'>
<thead>
<th>Title</th>
<th>Boolean 1</th>
<th>Boolean 2</th>
</thead>
{% if model1_list %}
{% for Model1 in model1_list %}
<tbody>
<tr>
<td>{{ Model1.title }}</td>
<td>{{ Model1.boolean1 }}</td>
<td>{{ Model1.boolean2 }}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
I'd like to display the boolean values as checkboxes rather than text, and to be able to check and uncheck the values and have it be reflected in the database (i.e., if the database is checked, the value should be True).
What would be a way to implement this?
Just use IF's:
<div class = 'table-responsive'>
<table class='table table-striped'>
<thead>
<th>Title</th>
<th>Boolean 1</th>
<th>Boolean 2</th>
</thead>
{% if model1_list %}
{% for Model1 in model1_list %}
<tbody>
<tr>
<td>{{ Model1.title }}</td>
<td>
<div class="checkbox">
<label><input type="checkbox" {% if Model1.boolean1 %} checked {% endif %}>Boolean 1</label>
</div>
</td>
<td>
<div class="checkbox">
<label><input type="checkbox" {% if Model1.boolean2 %} checked {% endif %}>Boolean 2</label>
</div>
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
When the user clicks, you want to send this information to the DB without refreshing the page? If yes, you need to do an ajax request to set this information to DB using JSON.Something like that:
$.ajaxFunction = function(){
var boolean1 = $("#boolean1").val(),
boolean2 = $("#boolean2").val(),
csrf_token = $("#csrf_token").val();
data_send = {
"boolean1": boolean1,
"boolean2": boolean2,
"csrfmiddlewaretoken": csrf_token
};
//jquery
$.ajax({
type: "post",
url: $("/url/ajax/").val(),
data: data_send,
success: function(data_receive){
if(data['boolean1']==true){
...
}
...
}
});
}
If no, you can just do a to POST this information and refresh the page!
django table information displayed twice
Why is the Due date and time remaining appear twice? I am trying figure out what is that coming from. I am still learning django, slowly. Sometimes, I'm just having trouble.
Picture:
http://gyazo.com/cae11df54df3f558865a772529f97139.png
.html code
{% if toolsBorrowed %}
<table id="dataTable" width="100%" cellspacing="0" cellpadding="5" class="table table-hover">
<thead>
<tr>
<th>Tool Name</th>
<th>Owner</th>
<th>Due Date</th>
<th>Time Remaining</th>
</tr>
</thead>
<tbody id="myTable">
{% for tool in toolsBorrowed %}
{% query Request borrowerId=user.id as req %}
{% query userAcc pk=tool.owner as owner %}
<tr style="cursor: pointer;" onclick="document.location = '/view_tool/{{ tool.id }}/';">
<td>{{ tool.name }}</td>
<td>{% for o in owner %}{{ o.username }}{% endfor %}</td>
{% for r in req %}
<td>{{ r.dueDate }}</td>
<td>{{ r.dueDate | timeuntil }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<strong>You are not borrowing any tools.</strong>
{% endif %}
views.py
def borrowed(request):
context = {'toolsBorrowed': Tool.objects.filter(borrower = request.user.id),
'toolsLoan':Tool.objects.filter(owner = request.user.id).exclude(borrower = None),
'userAcc' : userAcc.objects,
'Request' : Request.objects,
}
return render(request, 'borrowed.html', context)
Any help would be great.