循环 Django 模板中的对象列表

发布于 2024-12-03 04:45:18 字数 1818 浏览 0 评论 0原文

似乎看不出我哪里出错了。请原谅我,因为我对此很陌生。我正在尝试显示模型中的 10 个最新对象。

这是我用来将所有这些对象放入列表中的循环:

 # 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

然后我将其渲染到页面,如下所示:

 c = RequestContext(request, {'form' : form, 'entries_list' : entries_list})
    return render_to_response("main.html", c)

这是我的 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 %}

这是 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)

看来循环正在工作。那里有正确数量的条目。只是模板标签不起作用。它们只是空白。因此,将对象放入列表中似乎工作得很好,它只是不会检索我的个人字段。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝有风,海豚有海 2024-12-10 04:45:18

有几件事。有一种方法可以让您对查询进行排序并仅获取前十个条目。它会比你拥有的循环更有效。

您的模板不起作用的原因是您引用的是列表而不是单个条目。它应该是:

{% 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 %}

一旦你让你的模板工作,尝试这个来获取你的entries_list:

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

这是有关排序和切片查询的文档:https://docs.djangoproject.com/en/dev/topics/db/queries

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

殤城〤 2024-12-10 04:45:18

那么你做了什么:
如果你了解C语言基础..
你的问题是打印数组的元素,所以你会像......

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

类似地,在上面的情况下,你迭代 entries_list 并将每个元素分配给变量 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 %}

当然,@CarL 为您提供了更好的解决方案,可以根据您的模型获取最新 10 个食谱。

So what you did:
If you know basic of C language..
Your problem is to print element of 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 are iterating over entries_list and assigning each element to the variable entries. 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 ofcourse @CarL has given you a better solution for getting latest 10 recipes in case of your models.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文