Issue
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>
Solution
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}
.
Answered By - Rvector
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.