I have created model, write views, and urls and html template file but can't show data when doing. If you know the reason please help me? this is 3 file in django project models.py
# from django.contrib.auth.models import User
from django.contrib.auth.models import User
from django.db import models
# from rest_framework.authtoken.admin import User
from schedule import settings
class GiaoVien(models.Model):
madotxep = models.ForeignKey(DotXep, on_delete=models.CASCADE)
hodem = models.CharField(default='', max_length=50)
ten = models.CharField(default='', max_length=20)
viettat = models.CharField(default='', max_length=20)
nhomgv = models.CharField(default='', max_length=20)
mamau = models.IntegerField(default=0)
stt = models.IntegerField(default=0)
ghichu = models.TextField(default='')
def __str__(self):
return str(self.id)
views.py
enter code here
from django.shortcuts import render, redirect
from inputdata.models import GiaoVien
def show(request):
queryset = GiaoVien.objects.all()
context= {'clientes': queryset}
return render(request, "Main/showdata.html", context)
showdata.html
{% extends 'base.html' %}
{% block title %}Show Giao Vien{% endblock %}
{% block content %}
<table>
<thead>
{% for field in clientes %}
<th>{{ field.label }}</th>
{% endfor %}
</thead>
<tbody>
<tr>
{% for field in clientes %}
<td>{{ field.value|default_if_none:'' }}</td>
{% endfor %}
</tr>
</tbody>
</table>
{% endblock content %}
CodePudding user response:
Your problem is that you don't have a "label" field or a "value" field on your model. So you are not printing anything because this fields don't exist. Try something like this:
{% for field in clientes %}
<td>{{ field.madotxep |default_if_none:'' }}</td>
<td>{{ field.hodem |default_if_none:'' }}</td>
<td>{{ field.ten |default_if_none:'' }}</td>
<td>{{ field.viettat |default_if_none:'' }}</td>
{% endfor %}
CodePudding user response:
django template itself don't render the table for you with label or value, you need to specify the table header and it's contents in the loop yourself:
<table>
<thead>
<th>id</th>
<th>madotxep</th>
<th>hodem</th>
<th>ten</th>
<th>viettat</th>
<th>nhomgv</th>
<th>mamau</th>
<th>stt</th>
<th>ghichu</th>
</thead>
<tbody>
{% for client in clientes %}
<tr>
<td>{{ client.id|default_if_none:'' }}</td>
<td>{{ client.madotxep|default_if_none:'' }}</td>
<td>{{ client.hodem|default_if_none:'' }}</td>
<td>{{ client.ten|default_if_none:'' }}</td>
<td>{{ client.viettat|default_if_none:'' }}</td>
<td>{{ client.nhomgv|default_if_none:'' }}</td>
<td>{{ client.mamau|default_if_none:'' }}</td>
<td>{{ client.stt|default_if_none:'' }}</td>
<td>{{ client.ghichu|default_if_none:'' }}</td>
</tr>
{% endfor %}
</tbody>
</table>