'I can't display data from database on web page using Python and Django
I have a big problem. I want to display data from my database( SQLite) on a webpage( the data should be present in an HTML table). I have checked my request with Postman and there everything looks fine, but with all of these, I can't see the data on my page.( I'm using Python and Django)
**HTML from my template - **
<div id="patientsPanel">
<button class="addButton outline-text" style="top:5px;background-color:#FED558;" onclick="addNewPatient()"><i class="fas fa-plus outline-text" style="color:white;"></i> Add patient</button>
<button class="addButton outline-text" style="top:33px;background-color:#691188;text-align:center;" onclick="displayPatientsList()"><i class="fas fa-align-justify" style="color:white"></i> Patients</button>
<span class="closebtn outline-text" onclick="this.parentElement.style.display='none';">×</span>
<h1 class="outline-text" style=text-align:center>{{user.username}}</h1>
<div id="patientsList">
<table class="table">
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Phone</th>
<th>Actions</th>
</tr>
<tbody>
{% for patient in patientsForm %}
<tr>
<td>{{patient.firstName}}</td>
<td>{{patient.lastName}}</td>
<td>{{patient.phone}}</td>
<td>More</td>
</tr>
{% endfor %}
<tr>
<td>Dummy</td>
<td>Dummy</td>
<td>Dummy</td>
<td>More</td>
</tr>
</tbody>
</table>
</div>
</div>
There are two panels. When I click a button on the first one, the second panel( this is the panel with data from the database) should appear. The function displayPatientsList() is just for making the panel visible, nothing special.
Here is my views.py
def viewPatients(request):
print("Aici in viewPatients!!")
patients = Patient.objects.all()
return render(request, 'medRelations/account.html', {'patientsForm': patients})
urls.py
from django.contrib import admin
from django.urls import include, path
from medRelations.views import (
registrationView,
loginPageView,
accountView,
authView,
newPatient,
newIntervention,
viewPatients,
viewInterventions,
)
urlpatterns = [
path('', registrationView, name='registration'),
path('admin/', admin.site.urls),
path('loginPage', loginPageView, name='loginPageView'),
path('home', accountView, name='myAccount'),
path('login', authView, name='userLogin'),
path('newPatient', newPatient, name='newPatient'),
path('newIntervention', newIntervention, name='newIntervention'),
path('viewPatients', viewPatients, name='viewPatients'),
path('viewInterventions', viewInterventions, name='viewInterventions'),
]
Here is Postman. I can see the data from database in the HTML code when I send the request
I can't understand why this is not functional. I tried to search on Youtube and there some people used the same method as me, but for them it was successful.
Solution 1:[1]
Your view for patient list is not registered on /home
but on /viewPatients
.
Try accessing this path, everything else seems ok.
If this doesn't fix it, output a unique string to the template to check if it's the template being called (like TEST STRING
).
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 |