英文:
How do I use django templates to get a field from the parent object of a model?
问题
以下是要翻译的内容:
"我正在尝试创建一个界面,让学生可以根据共同的兴趣与班级中的其他学生匹配,以进行小组项目。我在Django视图中有一个查询集(queryset),然后我可以使用select_related获取父对象,并在Anaconda Prompt中打印字段(在本地服务器上运行时),但似乎无法在模板中显示该字段...我漏掉了什么?
这是我的模型文件的相关部分:
class Profile(models.Model):
first_nm = models.CharField(max_length=100)
last_nm = models.CharField(max_length=100)
class Potential(models.Model):
user1 = models.ForeignKey(Profile, related_name='user1', on_delete=models.CASCADE)
user2 = models.ForeignKey(Profile, related_name='user2', on_delete=models.CASCADE)
这是我的视图的相关部分:
def matches(request):
my_user = request.user
my_id = Profile.objects.get(slug=my_user)
the_matches = Potential.objects.filter(Q(user1=my_id) | Q(user2=my_id)).all().select_related('user1','user2')
print(the_matches.values())
print(the_matches.values('user2__first_nm'))
return render(request,'projects/matches_list.html', { 'matches':the_matches})
这是模板的相关部分:
{% for match in matches %}
<div class="matches">
<p>{{ match.user1.first_nm }}</p>
<p>{{ match.user2.first_nm }}</p>
</div>
{% endfor %}
在上面的模板中,为什么这不起作用?
提前感谢您的帮助!
我尝试使用如上所示的语法,例如:
<p>{{ match.user2.first_nm }}</p>
并且当仅在控制台中打印时,我得到
<QuerySet [{'user2__first_nm': 'test_nm'}]>
从上面视图中的倒数第二行,但当我在本地运行时,HTML显示为空的p标签。我做错了什么?
谢谢!
<details>
<summary>英文:</summary>
I'm trying to create an interface where students can match with other students in the class for a group project based on shared interests and I have a queryset in a django view and from there I am able to use select_related to get the parent object and even print the fields in Anaconda Prompt (when running on local server) but I can't seem to get the field to show up in a template...what am I missing?
Here is the relevant part of my models file:
class Profile(models.Model):
first_nm = models.CharField(max_length=100)
last_nm = models.CharField(max_length=100)
class Potential(models.Model):
user1 = models.ForeignKey(Profile, related_name='user1', on_delete=models.CASCADE)
user2 = models.ForeignKey(Profile, related_name='user2', on_delete=models.CASCADE)
Here is the relevant part of my view:
def matches(request):
my_user = request.user
my_id = Profile.objects.get(slug=my_user)
the_matches = Potential.objects.filter(Q(user1=my_id) | Q(user2=my_id)).all().select_related('user1','user2')
print(the_matches.values())
print(the_matches.values('user2__first_nm'))
return render(request,'projects/matches_list.html', { 'matches':the_matches})
Here is the relevant part of the template:
{% for match in matches %}
<div class="matches">
<p>{{ match.user1__first_nm }}</p>
<p>{{ match.user2__first_nm }}</p>
</div>
{% endfor %}
In the template above, why does this not work?
Thanks in advance!
I have tried using the syntax above like:
<p>{{ match.user2__first_nm }}</p>
and when just printing to the console I get
> <QuerySet [{'user2__first_nm': 'test_nm'}]>
from the second to last line in the view above but when I run locally, the html shows up with p tag with no contents. What am I doing wrong?
Thanks!
</details>
# 答案1
**得分**: 1
在模板中不要使用`__`,而是使用`.`。
`__` 仅在构建查询集时使用。在您的模板中,您正在访问`Profile`对象,因此要从该对象中获取属性,您需要使用语法 `object.attribute`。
您的代码将变为:
```python
{% for match in matches %}
<div class="matches">
<p>{{ match.user1.first_nm }}</p>
<p>{{ match.user2.first_nm }}</p>
</div>
{% endfor %}
英文:
Do not use __
in template, but use .
__
is only used when constructing QuerySets. In your template, you're accessing the Profile
object, so to get an attribute from that object you use the syntax object.attribute
.
Your code becomes:
{% for match in matches %}
<div class="matches">
<p>{{ match.user1.first_nm }}</p>
<p>{{ match.user2.first_nm }}</p>
</div>
{% endfor %}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论