1

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

3
  • Replace query_results with tablelst Commented Jul 18, 2021 at 5:56
  • i replaced it but still doesnt work Commented Jul 18, 2021 at 6:29
  • try to put {{tablelst}} in view, if it not working, you have to check from views.py or urls.py andr you can print(tablelst) in views.py and check console in terminal to verify it is working Commented Jul 18, 2021 at 6:38

1 Answer 1

1

in your views.py u are passing context so for rendering it in html you hav to pass

def table_list(request):
    company_list = CompanyRate.objects.all()
    context = {'company_list': company_list }
    return render(request, 'articleapp/table_list.html', context)

so in you html pass that context

{% for companyrate in company_list %}
        <tr>
            <td>{{ companyrate.id }}</td>
            <td>{{ companyrate.date }}</td>
            <td>{{ companyrate.notice_name }}</td>
            <td>{{ companyrate.event }}</td>
            <td>{{ companyrate.basic_amount }}</td>
            <td>{{ companyrate.num_of_company }}</td>
            <td>{{ companyrate.avg_of_1365 }}</td>
            <td>{{ companyrate.hd_rate }}</td>
            <td>{{ companyrate.hd_num }}</td>
            <td>{{ companyrate.hj_rate }}</td>
            <td>{{ companyrate.hj_num }}</td>
            <td>{{ companyrate.hm_rate }}</td>
            <td>{{ companyrate.hm_num }}</td>
            <td>{{ companyrate.url }}</td>
        </tr>
    {% endfor %}

you have to call the model with his name and in your models it showing you model name is companyrate

Sign up to request clarification or add additional context in comments.

5 Comments

i replaced query_result -> tablelst but still doesn't work
did you import you modes in your views.py
and why are u suing article.id model name is company rate i am edit my answer
oh it supposed to be erased. yes i imported my model in views.py
is you models is visible in admin panel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.