'Bootstap DataTable showing No matching records found Django
I am developing a project in Django where users can share files. I retrieve data(files) from the database and show it in a table on the template and use bootstrap DataTable to implement search functionality in my table But when I search any record from DataTable it shows me No matching records found.
Bootstrap Datatable CSS CDN
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.5/css/dataTables.bootstrap4.min.css">
Bootstrap Datatable Javascript CDN
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.11.5/js/dataTables.bootstrap4.min.js"></script>
Bootstrap script to show data table functionality in a HTML table
<script>
$(document).ready(function() {
$('#datatable').DataTable();
} );
</script>
Template Code:
{% extends 'instructor/admin_base.html' %}
{% load static %}
{% block body %}
<div id="page-container" >
<main id="main-container">
<div class="content">
<h3 class="text-center notesText">All Notes</h3><br>
`<div class="tablecss container mt-5" ><div >
<!--id="datatable" is used for implementing boostrap dataTable features in table-->
<table id="datatable" class=" table table-bordered table-striped table-vcenter js-dataTable-full-pagination" style=" color:gray;">
<thead style="background-color : #607d8b; color:white;" >
<tr>
<th>S.No</th>
<th>Uploading Date</th>
<th>Branch</th>
<th>Subject</th>
<th>Download Notes</th>
<th>File Type</th>
<th>Description</th>
<th>Status</th>
<th>Action</th>
</tr>
</th
{% for i in notes %}
<tbody>
<tr>
<td>{{forloop.counter}}</td>
<td>{{i.uploadingdate}}</td>
<td>{{i.branch}}</td>
<td>{{i.subject}}</td>
<td><a href="{{i.notesfile.url}}" class="btn btn-success" download>Download</a></td>
<td>{{i.filetype}}</td>
<td>{{i.description}}</td>
<td>{{i.status}}</td>
<td><a href="{% url 'dashboard:delete_notes' i.id %}" class="btn btn-danger" onclick="return confirm('Are your sure to Delete ?')">Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>`</div>`
</main>
</div>
Views.py (In which I'd like to implement search functinality)
def all_notes(request):
if not request.user.is_authenticated:
return redirect('login_admin')
notes = Upload_Notes.objects.all()
context = {'notes':notes}
return render(request, 'instructor/all_notes.html',context)
dashboard/urls.py:
from django.contrib import admin
from django.urls import path
from . import views
app_name="dashboard"
urlpatterns=[
path('pending_notes', views.pending_notes, name='pending_notes'),
path('assign_status/<int:pk>', views.assign_status, name='assign_status'),
path('accepted_notes', views.accepted_notes, name='accepted_notes'),
path('rejected_notes', views.rejected_notes, name='rejected_notes'),
path('all_notes', views.all_notes, name='all_notes'),
path('admin_home',views.admin_home,name="admin_home"),
path("showallusers", views.show_all_users, name="showallusers"),
]
Models.py
class Upload_Notes(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
uploadingdate = models.CharField(max_length=30)
branch = models.CharField(max_length=30)
subject = models.CharField(max_length=50)
notesfile = models.FileField(null=True,validators=(validate_file_extension,))
filetype = models.CharField(max_length=30)
description = models.CharField(max_length=200, null=True)
status = models.CharField(max_length=15)
def __str__(self):
return f'{self.user} notes'
delete_notes view
def delete_notes(request,pk=None):
if not request.user.is_authenticated:
return redirect('login')
notes = Upload_Notes.objects.get(id=pk)
notes.delete()
messages.success(request,f" Notes delated successfully!")
return redirect('/all_notes')
Like all_notes I have 3 other method for displaying pending_notes ,accepted_notes and rejected_notes.
Template image with error message no matching record found

Extra: Datatable is working perfectly in another table where I am showing all user info.I am not able to find the problem why data table is not working in the notes table.
Solution 1:[1]
consider the first line that tries to modify the data in v:
cin >> v[i].name;
This is occurring while v is an empty vector (has a size of 0). To add elements to the end of a vector you need to use push_back:
string name;
cin >> name;
v.push_back(name);
the square brackets ("[]") are for accessing an element that already exists in the vector.
Solution 2:[2]
In read_data you access the std::vector<Things> (Forest) v out of bounds (out of range) since the vector is empty.
You could resize it after having asked the user for how many elements that should be entered.
Example:
void read_data(Forest& v) {
int n;
if(std::cin >> n && n > 0) v.resize(n); // resize
for (int i = 0; i < v.size(); ++i) { // use v.size() here
std::cout << "DINTRE\n";
std::cin >> v[i].name;
std::cout << v[i].name << '\n';
}
}
or simpler by using a range-based for loop:
void read_data(Forest& v) {
int n;
if(std::cin >> n && n > 0) v.resize(n);
for(auto& thing : v) {
std::cout << "DINTRE\n";
std::cin >> thing.name;
std::cout << thing.name << '\n';
}
}
Solution 3:[3]
The size of vector, passed in read_data(), is 0 i.e. it's an empty vector. In read_data(), when you doing v[i] that means you are trying to access ith element of vector v which does not exists. Hence, you are getting the error. To fix this, create an object of type Things, take user input and push_back() it in vector v:
for (int i = 0; i < n; ++i) {
Things x;
cout << "DINTRE" << endl;
cin >> x.name;
cout << x.name << endl;
v.push_back(x);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eitan |
| Solution 2 | |
| Solution 3 | H.S. |
