简体   繁体   English

循环Django模板中的对象列表

[英]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. 我试图在模型中显示10个最新的对象。

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: 这是我的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: 这是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. 所以看起来这很好地将对象放在列表中,它只是不会检索我的单个字段。

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:

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 这是关于排序和切片查询的文档: https//docs.djangoproject.com/en/dev/topics/db/queries

So what you did: If you know basic of C language.. Your problem is to print element of array, So you will go like.. 所以你做了什么:如果你知道C语言的基础..你的问题是打印数组的元素,所以你会喜欢..

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 are iterating over entries_list and assigning each element to the variable entries . 类似地,在上面的例子中,您将迭代entries_list并将每个元素分配给变量entries Now you will play with entries . 现在你将玩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 ofcourse @CarL has given you a better solution for getting latest 10 recipes in case of your models. @CarL为您提供了更好的解决方案,以便在您的模型中获得最新的10种食谱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM