Answer the question
In order to leave comments, you need to log in
How to get the latest record from a database and display it in a table?
I use django 4, on one of the pages of the site I have a table that displays data from the database based on the model I built. To display a table in html, I use the django-tables2 library. Here are some of my settings in views.py:
from django.shortcuts import render
from .models import Titles
def analytics(request):
return render(request, 'main/analytics.html', {'analytics':Titles.objects.all()})
ValueError: Expected table or queryset, not Titles
Answer the question
In order to leave comments, you need to log in
It is written - the table or queryset is expected, and last returns one record. Most likely - in tables2 there is a separate method for displaying one record, you just need to google it.
However, if you need to solve the problem "on the forehead", then you can do this:
But this, IMHO, is shitty code.
Titles.objects.all().order_by('-id')[:1]
ValueError: Expected table or queryset, not Titlesstart by understanding the difference between a queryset and a particular record. Also carefully read https://docs.djangoproject.com/en/4.0/ref/models/q...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question