在Django模板中如何显示* linked *表中的字段值? [英] How to display value of a field in a *linked* table, in a Django template?
问题描述
我正在创建一个演示Django网站,您可以浏览Billy Joel的专辑。您点击相册查看所有歌曲。
I am creating a demo Django website where you browse Billy Joel's albums . You click on an album to view all songs.
(如果恰好是: http://104.131.200.120/albums/all /
- 目前几乎没有任何数据,冷泉港只有一首歌,无辜者中没有)
(If it happens to be on: http://104.131.200.120/albums/all/
--there's hardly any data at the moment. Only one song in Cold Spring Harbor, none in An Innocent Man)
我可以'了解如何在相册页面中显示歌曲名称。它可以告诉你是否有一首歌,因为冷泉港打印出一个空的名字/描述行,而一个无辜的人没有。
I can't figure out how to display the song name in the album page. It can tell if there is a song, because Cold Spring Harbor prints out an empty "name/description" line, and An Innocent Man doesn't.
这是视图
def album(request, album_id=1):
album = Album.objects.get(id=album_id)
album_songs = AlbumSong.objects.filter(id=album_id)
print(str(album_songs))
return render_to_response("album.html",
{"album": album, "album_songs" : album_songs})
( print
结果 [ColdFlow Spring Harbor:10:Got To Begin Again>]
正在写入控制台,当Cold弹簧港被选中。)
(the print
results in [<AlbumSong: Cold Spring Harbor: 10: Got To Begin Again>]
being written to the console, when Cold Spring Harbor is selected.)
该模型(请注意,歌曲和专辑模型没有通过外键链接,有一个中间表AlbumSong):
The model (note that the Song and Album models are not linked by foreign key. There's an intermediate table, AlbumSong):
from django.db import models
from django.contrib.auth.models import User
from time import time
def get_upload_file_name(instance, filename):
return "uploaded_files/%s_%s" % (str(time()).replace(".", "_"), filename)
class Album(models.Model):
title = models.CharField(max_length=70)
pub_date = models.DateField('release date')
is_official = models.BooleanField(default=False)
is_concert = models.BooleanField(default=False)
likes = models.IntegerField(default=0)
thumbnail = models.FileField(upload_to=get_upload_file_name, blank=True, null=True)
def __str__(self):
return self.title
class Song(models.Model):
name = models.CharField(max_length=100)
sub_name = models.CharField(max_length=200, null=True, blank=True)
length_seconds = models.IntegerField()
lyrics_url = models.TextField(default="", blank=True, null=True)
def __str__(self):
return self.name
class AlbumSong(models.Model):
song_id = models.ForeignKey(Song)
album_id = models.ForeignKey(Album)
sequence_num = models.IntegerField()
class Meta:
unique_together = ('album_id', 'sequence_num',)
unique_together = ('album_id', 'song_id',)
def __str__(self):
return str(self.album_id) + ": " + str(self.sequence_num) + ": " + str(self.song_id)
class FavoriteSongs(models.Model):
user_id = models.ForeignKey(User)
song_id = models.ForeignKey(Song)
sequence_num = models.IntegerField()
class Meta:
unique_together = ('user_id', 'song_id',)
unique_together = ('user_id', 'sequence_num',)
def __str__(self):
return "user=" + str(self.user_id) + ", song=" + str(self.song_id) + ", number=" + str(self.sequence_num)
class FavoriteAlbums(models.Model):
user_id = models.ForeignKey(User)
album_id = models.ForeignKey(Album)
sequence_num = models.IntegerField()
class Meta:
unique_together = ('user_id', 'album_id',)
unique_together = ('user_id', 'sequence_num',)
def __str__(self):
return "user=" + str(self.user_id) + ", album=" + str(self.album_id) + ", number=" + str(self.sequence_num)
和模板:
{% extends "base.html" %}
{% block title %}Album detail{% endblock %}
<!-- <div id="sidebar"> -->
{% block sidebar %}
<UL>
<LI><a href="/albums/all">Albums</A></LI>
</UL>
{% endblock %}
<!-- </div> -->
{% block content %}
<H1>{{ album.title }}</H1>
<P>{{ album.body }}</P>
{% if album.thumbnail %}
<P><img src="/static/{{ album.thumbnail }}" width="200"/></P>
{% endif %}
<P><UL>
<LI>Released: {{ album.pub_date }}</LI>
<LI>Official: {{ album.is_official }}</LI>
<LI>Concert: {{ album.is_concert }}</LI>
</UL></P>
<H2>Songs</H2>
{% for album_song in album_songs %} <!-- No colon after "album_songs" -->
<div>
<P><UL>
<LI>Name: <a href="/songs/get/{{ song.id }}">{{ album_song.song.name }}</a><UL>
<LI>Description: {{ album_song.song.sub_name }}</LI>
</UL></LI>
</UL></P>
</div>
{% endfor %}
<P><a href="/albums/like/{{ album.id }}">Like this album</A> -- {{ album.likes }} people liked this album.</P>
{% endblock %}
如何在模板? album_song.song.name
应该更改为?
How do I display the song name in the template? What should album_song.song.name
be changed to?
谢谢。
推荐答案
问题是您的字段名称。由于某些原因,您已经从AlbumSongsong_id和album_id中调用了外键。所以你应该在模板中使用相同的名字: {{album_song.song_id.name}}
。
The problem is your field names. For some reason, you've called your foreign keys from AlbumSong "song_id" and "album_id". So you should use the same names in the template: {{ album_song.song_id.name }}
.
使用这个名字是没有意义的。 Django字段不代表一个ID,它代表了实际的Song对象。 Django已经创建了一个带有_id后缀的底层数据库字段,所以在这种情况下,它创建了一个名为song_id_id,这是愚蠢的。重命名您的字段歌曲和相册,并保持模板的方式。
However, it doesn't make sense to use this name. The Django field does not represent an ID, it represents the actual Song object. Django already creates an underlying database field with an "_id" suffix, so in this case it's created one called "song_id_id", which is silly. Rename your fields "song" and "album", and keep your template the way it is.
这篇关于在Django模板中如何显示* linked *表中的字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!