Как мне отобразить определенную информацию в html странице django из базы sqlite3?

Создаю тестовый проект в Django rest api. Есть данные из sqlite3:

[
    {
        "id": 1,
        "title": "test",
        "content": "test",
        "idcard": "Card",
        "idmenu": "Info",
        "author": "Admin",
        "date": "3"
    },
    {
        "id": 2,
        "title": "Name table1",
        "content": "table1 will be here",
        "idcard": "Card",
        "idmenu": "Info",
        "author": "-",
        "date": "-"
    },
    {
        "id": 3,
        "title": "Name article1",
        "content": "article1 will be here",
        "idcard": "Card",
        "idmenu": "Info",
        "author": "-",
        "date": "-"
    },
    {
        "id": 4,
        "title": "Name table41",
        "content": "table41 will be here",
        "idcard": "MiniCard",
        "idmenu": "Info",
        "author": "-",
        "date": "-"
    },
    {
        "id": 5,
        "title": "Name table2",
        "content": "table2 will be here",
        "idcard": "Card",
        "idmenu": "Start",
        "author": "-",
        "date": "-"
    },
    {
        "id": 6,
        "title": "Name table3",
        "content": "table3 will be here",
        "idcard": "MiniCard",
        "idmenu": "Start",
        "author": "-",
        "date": "-"
    },
    {
        "id": 7,
        "title": "Name table5",
        "content": "table5 will be here",
        "idcard": "MiniCard",
        "idmenu": "Start",
        "author": "-",
        "date": "-"
    },
    {
        "id": 8,
        "title": "Name table6",
        "content": "table6 will be here",
        "idcard": "MiniCard",
        "idmenu": "2G",
        "author": "-",
        "date": "-"
    },
    {
        "id": 9,
        "title": "Name table7",
        "content": "table7 will be here",
        "idcard": "Card",
        "idmenu": "2G",
        "author": "-",
        "date": "-"
    }
]

Я знаю что если в html отобразить таким образом код из базы данных:

    <div class="col-sm-6 col-md-9" style="overflow-y: scroll; height:500px;">
        <div class="card">        
        {% for item in contents_list %}
        <div class="card-body">
            <h5>Title1 {{ item.title }}</h5>
            <p>Content1 {{ item.content }}</p>
        </div>
        {% endfor %}        
        </div>
    </div>

Код у меня отобразится корректно в html странице. то есть у меня отобразится в странице

Title1 test
Content1 test
Title1 Name table1
Content1 table1 will be here
Title1 Name article1
Content1 article1 will be here
Title1 Name table41
Content1 table41 will be here
Title1 Name table2
Content1 table2 will be here
Title1 Name table3
Content1 table3 will be here
Title1 Name table5
Content1 table5 will be here
Title1 Name table6
Content1 table6 will be here
Title1 Name table7
Content1 table7 will be here

Но как мне отобразить определенную информацию, например из idmenu. то есть мне нужно отобразить такую информацию:

Info
Start
2G

Вот мой код views:

from django.shortcuts import render
from .models import Content
from rest_framework import viewsets
from .serializers import ContentSerializer

#from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.renderers import TemplateHTMLRenderer

# Create your views here.
class ContentViewSet(viewsets.ModelViewSet):
    queryset = Content.objects.all()
    serializer_class = ContentSerializer

class MyDRFHTMLView(APIView):
    renderer_classes = [TemplateHTMLRenderer]
    #template_name = 'webPages/index.html' # Specify the template
    #def index(request):
    #    print('Кто-то зашёл на главную!')
    #    return render(request, 'webPages/index.html')
    #def get(self, request, *args, **kwargs):
    #    # You can pass context data to the template
    #    context = {'message': 'Hello from DRF!'}
    #    return Response(context)    
    #def get(self, request):
    #    print('Кто-то зашёл на главную!')
    #    return render(request, 'webPages/index.html')    
    def get(self, request):
        print('Кто-то зашёл на главную!')
        contents_list = Content.objects.all() # Получить все объекты из модели        
        print(contents_list)
        '''json_data = {}
        for raw in contents_list:
            #print(raw.idmenu)
            if raw.idmenu not in json_data:
                json_data[raw.idmenu] = []
            json_data[raw.idmenu].append({
                'id': raw.id,
                'title': raw.title,
                'content': raw.content,
                'idcard': raw.idcard,
                'idmenu': raw.idmenu,
                'author': raw.author,
                'date': raw.date
            })
        print(json_data)'''


        context = {'contents_list': contents_list}
        
        #context = {'json_data': json_data}
        return render(request, 'webPages/index.html', context)
        #return render(request, 'webPages/index.html', json_data=json_data)

В комментариях это попытки реализовать то чего я добиваюсь, но не получилось. На всякий случай покажу models:

from django.db import models
class Content(models.Model):
    title = models.CharField(max_length=255, verbose_name="Название")
    content = models.CharField(max_length=255, verbose_name="Контент")
    idcard = models.CharField(max_length=255, verbose_name="Карта")
    idmenu = models.CharField(max_length=255, verbose_name="Меню")             
    author = models.CharField(max_length=255, verbose_name="Автор поста")
    date = models.CharField(max_length=255, verbose_name="Дата поста")
    def __str__(self):
        return self.title

и urls:

#from django.urls import path
from django.urls import include, path
#from . import views
from rest_framework.routers import DefaultRouter
from .views import ContentViewSet, MyDRFHTMLView
#urlpatterns = [
#    path('', views.index, name='index'), # Связываем корень (пустую строку) с функцией index
#]
router = DefaultRouter()
router.register(r'content', ContentViewSet)

#urlpatterns = router.urls
urlpatterns = [
    #path('', include(router.urls)), #/urls/
    path('', include(router.urls)), #/urls/
    path('info/', MyDRFHTMLView.as_view(), name='info'),
    #path('data/', views.get, name='display_data'),
]

Может я неправильно в модели добавляю данные?

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