Tuesday, February 28, 2023

Re: How to retrieve the latest object added (new one) in Django models and display it in the template?

Se comments below.


Den fre 24 feb. 2023 kl 12:14 skrev Byansi Samuel <samuelbyansi87@gmail.com>:

Hey everyone, l got a problem. Am trying to retrieve a single latest object added everytime, and display it in the template.
Below is my codes but;

Help me figure it out the source of the problem in these different instances below 👇

_______________________ issue number 1_______

#Action/models.py
class ActionGame(models.Model):
        name=models.Charfield()
        published=models.DateTimeField()

#Action/views.py
from Action.models import ActionGame

def action (request):
        latest_action=ActionGame.objects.filter(published=published). latest()
Context={ 'latest_action': latest_action }
return....

But it returns *error* :
name 'published' is not defined


You haven't defined what the value of published is. If you want to get the last created one the query should be:
latest_action=ActionGame.objects.latest("published") - that would give you the last published item. Another way would be:
latest_action=ActionGame.objects.order_by("published").last()
 

____________________ issue number 2________

#Action/models.py
class ActionGame(models.Model):
        name=models.Charfield()
        published=models.DateTimeField()
        class Meta:
              get_latest_by='published'

#Action/views.py
........
latest_action=ActionGame.objects. latest()
.......

#but it returns *error* :
'ActionGame' object is not iterable

Even if I try this:
............
latest_action=ActionGame.objects. latest('published')
.......

#it returns the Same error:
'ActionGame' object is not iterable

But this second issue, the error is referred in #action.html

{% for x in latest _action %}
<p>{{ x.game_name }}</p>
{% endfor %}


So the latest_action variable that you have sent to the template is an instance of ActionGame - and not a list or anything. You can only access variables in the ActionGame class. For example:

 <p>{{ lastest_action.name }}</p>

would print out the value of the name of the ActionGame instance.

Please l need your assistance.

I'm Samuel,
Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGQoQ3wMUno6hLAO-1FtN0Nn7VtkCn4qf-O4U%3DpeJ4JzY%2B%3DcAQ%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCeBCST7o-5WZx%2BmhGO2yH-Msdse%3DKusk9UOyW8XZBXRBw%40mail.gmail.com.

No comments:

Post a Comment