I want to display the data from database using html table in django.
im using mysql and already connected to django.
here is my view :
def table_list(request):
tablelst = CompanyRate.objects.all()
context = {'tablelst': tablelst}
return render(request, 'articleapp/table_list.html', context)
Model:
class CompanyRate(models.Model):
date = models.CharField(max_length=255, blank=True, null=True)
notice_name = models.CharField(max_length=255, blank=True, null=True)
event = models.CharField(max_length=255, blank=True, null=True)
basic_amount = models.CharField(max_length=255, blank=True, null=True)
num_of_company = models.CharField(max_length=255, blank=True, null=True)
avg_of_1365 = models.CharField(max_length=255, blank=True, null=True)
hd_rate = models.CharField(max_length=255, blank=True, null=True)
hd_num = models.CharField(max_length=255, blank=True, null=True)
hj_rate = models.CharField(max_length=255, blank=True, null=True)
hj_num = models.CharField(max_length=255, blank=True, null=True)
hm_rate = models.CharField(max_length=255, blank=True, null=True)
hm_num = models.CharField(max_length=255, blank=True, null=True)
url = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'company_rate'
articleapp/urls.py
urlpatterns = [
path('table_list/', TemplateView.as_view(template_name='articleapp/table_list.html'),
name='table_list'),
]
main urls.py
urlpatterns = [
path('articles/', include('articleapp.urls')),
]
table_list.html
{% extends 'base.html' %}
{% block content %}
<table class="table table-hover table-responsive">
<tbody>
{% for article in tablelst%}
<tr>
<td>{{ article.date }}</td>
<td>{{ article.notice_name }}</td>
<td>{{ article.event }}</td>
<td>{{ article.basic_amount }}</td>
<td>{{ article.num_of_company }}</td>
<td>{{ article.avg_of_1365 }}</td>
<td>{{ article.hd_rate }}</td>
<td>{{ article.hd_num }}</td>
<td>{{ article.hj_rate }}</td>
<td>{{ article.hj_num }}</td>
<td>{{ article.hm_rate }}</td>
<td>{{ article.hm_num }}</td>
<td>{{ article.url }}</td>
</tr>
{% endfor %}
<tbody>
</table>
{% endblock %}
I did something like this but, td is not working. nothing happens. Please help
query_resultswithtablelst