chiseledCoder
chiseledCoder

Reputation: 385

Display data from models in website

I am new to Django. I want to display latest data entered from models to the website.

models.py

class Service(models.Model):
    service_name = models.CharField(max_length=200)
    service_code = models.IntegerField(default=0, unique=True)

views.py

def Latest(return):
    latest_services = Service.objects.order_by('service_name')

index.html

{{ service_name }}
Latest Services Goes here

When I run the code nothing is displayed!

Upvotes: 0

Views: 67

Answers (1)

cor
cor

Reputation: 3393

You need to iterate over the queryset:

<ul>
    {% for service in latest_services %}
        <li>{{ service.service_name }}</li>
    {% endfor %}
</ul>

Anyway, if you want to display the latest entries, you should add a new field to your model with the date. For example:

class Service(models.Model):
    created_on = models.DateTimeField(auto_now=True)
    service_name = models.CharField(max_length=200)
    service_code = models.IntegerField(default=0, unique=True)

And then in your query:

latest_services = Service.objects.order_by('created_on')

Upvotes: 1

Related Questions