我无法在 django 模板中显示此外键 table 值
I can't get this foreignkey table value to be displayed in the django template
我无法在我的模板中显示某些外键值。这是题中涉及的两个模型:
** Models.py **
class Portfolio(models.Model):
title = models.CharField(max_length= 50, null= True)
description = models.TextField(max_length= 300, null= True)
class Spotify_Playlist(models.Model):
portfolio = models.ForeignKey(Portfolio, on_delete= models.CASCADE, null= True)
title = models.CharField(max_length= 50)
spotify_url = models.CharField(max_length= 50)
description = models.TextField(max_length= 100, null= True)
artist_img = models.ImageField(null= True, upload_to= 'image/')
*** Admin.py ***
@admin.register(Portfolio)
class PortfolioAdmin(admin.ModelAdmin):
inlines = [
SpotyListInline,
SpotySingleInline,
YoutubeInline,
]
class SpotyListInline(admin.TabularInline):
model = Spotify_Playlist
所以我想在我的模板中像这样显示 spotify 播放列表
*** index.html ***
<section id="portfolio">
{% for portfolio in portfolios %}
<h1>{{ portfolio.title }}</h1>
<p>{{ portfolio.description }}</p>
{% for playlist in portfolio.Spotify_Playlist_set.all %}
<p>{{playlist.title}}</p>
<iframe src='https://open.spotify.com/embed/playlist/{{ playlist.spotify_url }}'
width="50%" height="380" frameBorder="0" allowtransparency="true" allow="encrypted-media"></iframe>
{% endfor %}
{% endfor %}
</section>
但是 {{playlist.title}} 和带有 {{playlist.spotify_url}} 的嵌入式播放器没有显示。
我真的很感激任何帮助
默认的反向关系名称是小写的。尝试:
{% for playlist in portfolio.spotify_playlist_set.all %}
来自文档 https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward:
If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased. This Manager returns QuerySets, which can be filtered and manipulated as described in the “Retrieving objects” section above.
我无法在我的模板中显示某些外键值。这是题中涉及的两个模型:
** Models.py **
class Portfolio(models.Model):
title = models.CharField(max_length= 50, null= True)
description = models.TextField(max_length= 300, null= True)
class Spotify_Playlist(models.Model):
portfolio = models.ForeignKey(Portfolio, on_delete= models.CASCADE, null= True)
title = models.CharField(max_length= 50)
spotify_url = models.CharField(max_length= 50)
description = models.TextField(max_length= 100, null= True)
artist_img = models.ImageField(null= True, upload_to= 'image/')
*** Admin.py ***
@admin.register(Portfolio)
class PortfolioAdmin(admin.ModelAdmin):
inlines = [
SpotyListInline,
SpotySingleInline,
YoutubeInline,
]
class SpotyListInline(admin.TabularInline):
model = Spotify_Playlist
所以我想在我的模板中像这样显示 spotify 播放列表
*** index.html ***
<section id="portfolio">
{% for portfolio in portfolios %}
<h1>{{ portfolio.title }}</h1>
<p>{{ portfolio.description }}</p>
{% for playlist in portfolio.Spotify_Playlist_set.all %}
<p>{{playlist.title}}</p>
<iframe src='https://open.spotify.com/embed/playlist/{{ playlist.spotify_url }}'
width="50%" height="380" frameBorder="0" allowtransparency="true" allow="encrypted-media"></iframe>
{% endfor %}
{% endfor %}
</section>
但是 {{playlist.title}} 和带有 {{playlist.spotify_url}} 的嵌入式播放器没有显示。 我真的很感激任何帮助
默认的反向关系名称是小写的。尝试:
{% for playlist in portfolio.spotify_playlist_set.all %}
来自文档 https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward:
If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased. This Manager returns QuerySets, which can be filtered and manipulated as described in the “Retrieving objects” section above.