Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping over a list of objects within a Django template

Can't seem to see where I am going wrong here. Forgive me because I am new to this. I am trying to display the 10 newest objects within a model.

Here is the loop I used to put all of these objects within 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

I then render this to the page like so:

 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 that the loop is working. The correct amount of entries are there. It is just that the template tags aren't working. They are just blank. So it seems this is working just fine putting the objects inside the list, it just won't retrieve my individual fields.

like image 558
john Avatar asked Sep 05 '11 17:09

john


People also ask

What does {% include %} do in Django?

From the documentation: {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

What does {% include %} do?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.

What is Forloop counter in Django?

All the variables related to the counter are listed below. forloop. counter: By using this, the iteration of the loop starts from index 1. forloop. counter0: By using this, the iteration of the loop starts from index 0.


1 Answers

A couple of things. There is a method by which you can order your query and just get the first ten entries. It would be more efficient than the loop you have.

The reason your template doesn't work is that you're referring to the list rather than the individual 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 get your template working, try this to get your entries_list:

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

Here's the docs on sorting and slicing queries: https://docs.djangoproject.com/en/dev/topics/db/queries

like image 101
Carl F. Avatar answered Sep 27 '22 17:09

Carl F.