Невозможно получить данные из mysql с помощью django

Я не получаю данные в таблицу. Уверяю вас, что я не получил никаких ошибок при выполнении python manage.py runserver и мое соединение с базой данных Django работает отлично. Я также уверяю вас, что таблица в моей базе данных имеет адекватные данные и в базе данных нет никаких проблем.

Из файла 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)

Из 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)

Из 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/popper.js@1.16.0/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>

Вы допустили ошибку в шаблоне, {% for x in allcategories %} вместо {% for x in getTblchkone %}.

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

Потому что в контексте, который вы задали context = {'allcategories' : allcategories}.

Вернуться на верх