How to display information on the site sample? For example, a site that displays information about the last 5 who drove to the parking lot table: date time | car number | parking position

Question: how to show on the website the last 5 people who have come with built-in dango?

You can reply with links to the required doc.

Closed due to the fact that the essence of the question is incomprehensible by the participants Sergey Gornostaev , 0xdb , slippyk , Artem , Viktor Tomilov 30 Mar '18 at 8:47 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    In the view, form a query with data truncation and transfer to the template, then in the template output the data of this sample.

    For example:

    # models.py class Parking(models.Model): timestamp = models.DateTimeField(auto_now=True) car_number = models.CharField(max_length = 15) parking_position = models.CharField(max_length = 5) # views.py def cars_on_parking(request): cars = Parking.objects.all().order_by('-timestamp')[:5] .... .... order_by('-timestamp') - сортировка по полю timestamp. в обратном порядке [:5] - усечение данных в запросе, т.е. взять 5 записей из запроса. 

    Then in the cars variable there will be information about the last 5 entries from the Parking model.

    You can also use reverse () for the request instead of order_by ('- timestamp')

    You can read about reverse () the following link: https://docs.djangoproject.com/en/2.0/ref/models/querysets/#reverse

    • one
      Thank you so much, what you need, just did not know how to do it - Ivan Kakurkin