I donโt seem to see a place where I am mistaken. Forgive me because I'm new to this. I am trying to display 10 latest objects in a model.
Here is the loop that I used to place all of these objects in a list:
# put the top 10 newest Recipe objects in a list entries_list = [] all_recipes = Recipes.objects.annotate(Count('id')) newest_recipe_index = len(all_recipes) index = 0 while index < 10: try: x = Recipes.objects.get(id=newest_recipe_index) entries_list.append(x) newest_recipe_index = newest_recipe_index - 1 index = index + 1 except: index = index + 1 pass
Then I put it on the page as follows:
c = RequestContext(request, {'form' : form, 'entries_list' : entries_list}) return render_to_response("main.html", c)
And here is my html:
{% for entries in entries_list %} <i><b>Name:</i></b> {{ entries_list.name }}<br> <img src="/images{{ entries_list.picture }}" height="300" width="300"></img><br> <i><b>Ingredients:</i></b> {{ entries_list.ingredients }}<br> <p><i>{{ entries_list.description }}</i></p> <i><b>Created by:</i></b> {{ entries_list.user }}<br><br> {% endfor %}
And here is models.py:
class Recipes(models.Model): name = models.CharField(max_length=50) ingredients = models.CharField(max_length=300) picture = models.ImageField(upload_to = 'recipes/%Y/%m/%d') user = models.CharField(max_length=30) date = models.DateTimeField(auto_now=True) description = models.TextField() comments = models.ManyToManyField(Comments)
It seems the loop is working. The correct number of entries. Just the template tags do not work. They are just empty. So it seems that this works by simply placing the objects inside the list, it just wonโt retrieve my individual fields.
john
source share