Crossing a List of Objects in a Django Template

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.

+7
source share
2 answers

A few things. There is a method by which you can order your request and get the first ten records. This would be more efficient than the cycle you have.

The reason your template does not work is because you are referencing a list, not a separate entry. It should be:

 {% for entry in entries_list %} <i><b>Name:</i></b> {{ entry.name }}<br> <img src="/images{{ entry.picture }}" height="300" width="300"></img><br> <i><b>Ingredients:</i></b> {{ entry.ingredients }}<br> <p><i>{{ entry.description }}</i></p> <i><b>Created by:</i></b> {{ entry.user }}<br><br> {% endfor %} 

Once you have earned your template, try this to get your entry_list:

 entries_list = Recipes.objects.order_by('-id')[0:10] 

Here are the docs for sorting and cutting requests: https://docs.djangoproject.com/en/dev/topics/db/queries

+6
source

So what have you done: If you know the base language of C .. Your problem is to print the element of the array, so you will go like ..

 array = [1,2,3,4,5,6,7] int i=0; for(i=0;i<8;i++) { print i; // print array; is wrong } 

Similarly, in the above case, you iterate over entries_list and assign entries to each element of the variable. Now you will play with entries .

 {% for entries in entries_list %} <i><b>Name:</i></b> {{ entries.name }}<br> <img src="/images{{ entries.picture }}" height="300" width="300"></img><br> <i><b>Ingredients:</i></b> {{ entries.ingredients }}<br> <p><i>{{ entries.description }}</i></p> {% endfor %} 

And, of course, @CarL provided you with the best solution for getting the last 10 recipes for your models.

+1
source

All Articles