如何使用Django模板从模型的父对象中获取字段?

huangapple go评论103阅读模式
英文:

How do I use django templates to get a field from the parent object of a model?

问题

以下是要翻译的内容:

"我正在尝试创建一个界面,让学生可以根据共同的兴趣与班级中的其他学生匹配,以进行小组项目。我在Django视图中有一个查询集(queryset),然后我可以使用select_related获取父对象,并在Anaconda Prompt中打印字段(在本地服务器上运行时),但似乎无法在模板中显示该字段...我漏掉了什么?

这是我的模型文件的相关部分:

  1. class Profile(models.Model):
  2. first_nm = models.CharField(max_length=100)
  3. last_nm = models.CharField(max_length=100)
  4. class Potential(models.Model):
  5. user1 = models.ForeignKey(Profile, related_name='user1', on_delete=models.CASCADE)
  6. user2 = models.ForeignKey(Profile, related_name='user2', on_delete=models.CASCADE)

这是我的视图的相关部分:

  1. def matches(request):
  2. my_user = request.user
  3. my_id = Profile.objects.get(slug=my_user)
  4. the_matches = Potential.objects.filter(Q(user1=my_id) | Q(user2=my_id)).all().select_related('user1','user2')
  5. print(the_matches.values())
  6. print(the_matches.values('user2__first_nm'))
  7. return render(request,'projects/matches_list.html', { 'matches':the_matches})

这是模板的相关部分:

  1. {% for match in matches %}
  2. <div class="matches">
  3. <p>{{ match.user1.first_nm }}</p>
  4. <p>{{ match.user2.first_nm }}</p>
  5. </div>
  6. {% endfor %}

在上面的模板中,为什么这不起作用?

提前感谢您的帮助!

我尝试使用如上所示的语法,例如:

  1. <p>{{ match.user2.first_nm }}</p>

并且当仅在控制台中打印时,我得到

  1. <QuerySet [{'user2__first_nm': 'test_nm'}]>

从上面视图中的倒数第二行,但当我在本地运行时,HTML显示为空的p标签。我做错了什么?

谢谢!

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;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&#39;t seem to get the field to show up in a template...what am I missing?
  4. 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)

  1. 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})

  1. 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 %}

  1. In the template above, why does this not work?
  2. Thanks in advance!
  3. I have tried using the syntax above like:

<p>{{ match.user2__first_nm }}</p>

  1. and when just printing to the console I get
  2. &gt; &lt;QuerySet [{&#39;user2__first_nm&#39;: &#39;test_nm&#39;}]&gt;
  3. 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?
  4. Thanks!
  5. </details>
  6. # 答案1
  7. **得分**: 1
  8. 在模板中不要使用`__`,而是使用`.`
  9. `__` 仅在构建查询集时使用。在您的模板中,您正在访问`Profile`对象,因此要从该对象中获取属性,您需要使用语法 `object.attribute`
  10. 您的代码将变为:
  11. ```python
  12. {% for match in matches %}
  13. &lt;div class=&quot;matches&quot;&gt;
  14. &lt;p&gt;{{ match.user1.first_nm }}&lt;/p&gt;
  15. &lt;p&gt;{{ match.user2.first_nm }}&lt;/p&gt;
  16. &lt;/div&gt;
  17. {% 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:

  1. {% for match in matches %}
  2. &lt;div class=&quot;matches&quot;&gt;
  3. &lt;p&gt;{{ match.user1.first_nm }}&lt;/p&gt;
  4. &lt;p&gt;{{ match.user2.first_nm }}&lt;/p&gt;
  5. &lt;/div&gt;
  6. {% endfor %}

huangapple
  • 本文由 发表于 2023年1月6日 14:47:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定