0

I am not getting the data into the table. I assure you that I didn't get any errors while running python manage.py runserver and my database connection with Django is working perfectly. I also assure you that the table in my database has adequate data and there is no issue in the database.

From views.py:

from django.shortcuts import render, HttpResponse
from anapp.models import Tblchkone
# Create your views here.

def main(request):
    return render(request, 'main.html')

def getTblchkone(request):
    allcategories  = Tblchkone.objects.all()
    context = {'allcategories' : allcategories}
    return render(request, 'main.html', context)

From models.py:

from django.db import models
from django.db.models.base import Model

# Create your models here.

class Tblchkone(models.Model):
    categoryId = models.BigAutoField(primary_key=True, editable=False)
    categoryName = models.CharField(max_length=14, unique=True)

From main.html:

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
    integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

  <title>MAIN</title>
</head>

<body>
  <table class="table">
    <thead>
      <tr>
        <th scope="col">Category_Id</th>
        <th scope="col">Catefory_Name</th>
      </tr>
    </thead>
    <tbody>

      {% for x in getTblchkone %}
      <tr>
        <td>{{x.categoryId}}</td>
        <td>{{x.categoryName}}</td>
      </tr>

      {% endfor %}

    </tbody>
  </table>
  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
    integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
    crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
    integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
    crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
    integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
    crossorigin="anonymous"></script>
</body>

</html>
0

1 Answer 1

1

You make an error in the template, {% for x in allcategories %} instead of {% for x in getTblchkone %}.

{% for x in allcategories %}
<tr>
    <td>{{x.categoryId}}</td>
    <td>{{x.categoryName}}</td>
</tr>
{% endfor %}

Because in the context you set context = {'allcategories' : allcategories}.

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

3 Comments

I made this change but still I am not getting the result.
Very strange, can you add a print to your view ? To see if the queryset Tblchkone.objects.all() is not empty ? : print(Tblchkone.objects.all()) in the console !
Thanks! queryset is showing up. That means there is no obstacle between Django app and the database. There is something wrong at my own end.

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.