[英]how to get family tree using Django template and from ManyToMany field?
[英]How to acces the the django ManyToMany Field in django Template
我的模型中有一組屬性,其中一個屬性屬於多對多字段類型。 我能夠訪問模板中的所有屬性,而不是多對多字段之一。
我試過在我的模板中遵循
{% for post in all_posts %}
{{ post.likes }}
{% endfor %}
模型.py
class Posts(models.Model):
title = models.CharField(max_length=250, blank=False)
content = models.CharField(max_length=15000,
help_text="Write Your thought here...")
creation_time = models.DateTimeField(auto_now_add=True, editable=False)
likes = models.ManyToManyField(User, blank=True, related_name='likes')
視圖.py
def home(request):
template = loader.get_template('home.html')
all_posts = Posts.objects.all()
context = {
'all_posts': all_posts,
}
return HttpResponse(template.render(context, request))
當我使用{{ post.likes }}
,頁面上呈現的是 auth.User.None
您必須遍歷所選帖子的所有贊
嘗試這樣的事情:
{% for post in all_posts %}
{% for like in post.likes.all %}
{{ like }}
{% endfor %}
{% endfor %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.