类是玩家检索房屋名称中所有玩家的名字 机型:
class player(models.Model):
"""
Model representing a book genre (e.g. Science Fiction, Non Fiction).
"""
name = models.CharField(max_length=100, help_text="Enter a players name")
Id = models.CharField(primary_key=True,max_length=100, help_text="Enter a players Id")
Num_matches = models.IntegerField()
Num_baskets = models.IntegerField()
house_name = models.ForeignKey('House_name',on_delete=models.SET_NULL, null=True)
def __str__(self):
"""
String for representing the Model object (in Admin site etc.)
"""
return self.Id
视图
def shivalik(request):
set1 = player.objects.filter(house_name__hou_name='shivalik')
return render(request,'shivalik.html',
context={'set1':set1 }
)
html文件
{% block content %}
<h2>{{set1}}</h2>
{% end block %}
在shivalik.html中,它不会打印正在打印查询的播放器的名称 我试图在视图中保留set1.name,但它在集合中没有显示属性名称 请帮我显示house_name中所有玩家名称的列表:shivalik
答案 0 :(得分:0)
您需要使用for
标记在模板中打印名称。
{% for player in set1 %}
<p>{{player.name}}</p>
{% endfor %}
答案 1 :(得分:0)
您需要遍历玩家的查询集。像这样:
<强> shivalik.html 强>
{% for player in set1 %}
{{ player.name }}
{% endfor %}
只需从想要迭代的播放器模型中替换属性的name属性。