循环 Django 模板中的对象列表
似乎看不出我哪里出错了。请原谅我,因为我对此很陌生。我正在尝试显示模型中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事。有一种方法可以让您对查询进行排序并仅获取前十个条目。它会比你拥有的循环更有效。
您的模板不起作用的原因是您引用的是列表而不是单个条目。它应该是:
一旦你让你的模板工作,尝试这个来获取你的entries_list:
这是有关排序和切片查询的文档: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:
Once you get your template working, try this to get your entries_list:
Here's the docs on sorting and slicing queries: https://docs.djangoproject.com/en/dev/topics/db/queries
那么你做了什么:
如果你了解C语言基础..
你的问题是打印数组的元素,所以你会像......
类似地,在上面的情况下,你迭代
entries_list
并将每个元素分配给变量entries
。现在您将玩条目
。当然,
@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..
Similarly in the above case you are iterating over
entries_list
and assigning each element to the variableentries
. Now you will play withentries
.And ofcourse
@CarL
has given you a better solution for getting latest 10 recipes in case of your models.