i am trying to retrieve latest single data from Django model and show it in templates instead of all the data the method I trying to use in my view to achieve that
def preview(request):
readme = Personal_readme.objects.latest('create_date')
return render(request, 'preview.html',{'readme':readme})
and in my models
create_date = models.DateTimeField(default=timezone.now,editable=False)
but when I runserve and refer to a page it TypeError at /preview/ Personal_readme' object is not iterable it work fine when use this
readme = Personal_readme.objects.all()
but retrieve all the data in it but i want to retrieve single(one) the latest data based on create_date
I have no idea why its is show like this
CodePudding user response:
I guess your template waits list of items, and when you give it one, it cause to error.
So you can change your template to handle 1 item, or give it list
with 1 item like so:
def preview(request):
readme = [Personal_readme.objects.latest('create_date')]
return render(request, 'preview.html',{'readme': readme})