1

我正在尝试建立一个关于艺术家、歌曲和专辑的歌曲信息的基本数据库。在我的任何给定专辑的页面上,我试图显示给定专辑的专辑封面。我正在使用 Django Admin 中的内置图像上传功能来上传图像。图片网址已正确保存在 mysql 中(我检查过),但图片未显示在网站上。根据我在网上阅读的内容,可能存在问题的区域要么在 URL 文件中(我没有修改以适应媒体),要么在我的 MEDIA_URL 中,但我已按照 Django 概述的说明进行操作。下面我链接了我认为相关的代码。预先感谢您的帮助!

视图.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from Radio.models import Song, Artist, Album

def SongsAll(request):
    songs = Song.objects.all().order_by('songName')
    context = {'songs' : songs}
    return render_to_response('songsall.html', context, context_instance = RequestContext(request))

def SpecificSong(request, songname):
    song = Song.objects.get(songName = songname)
    context = {'song':song}
    return render_to_response('specificsong.html',context, context_instance=RequestContext(request))

def SpecificArtist(request, artistname):
    singer = Artist.objects.get(artistName = artistname)
    songs = Song.objects.filter(artist = singer)
    context = {'songs':songs}
    return render_to_response('specificartist.html',context, context_instance=RequestContext(request))

def SpecificAlbum(request, albumname):
    album = Album.objects.get(albumName = albumname)
    songs = Song.objects.filter(album = album)
    context = {'songs':songs}
    return render_to_response('specificalbum.html', context, context_instance=RequestContext(request))

settings.py(仅相关部分)

MEDIA_ROOT = '/home/kyle/Downloads/Django-1.5.1/radioSite/media/'

MEDIA_URL = 'http://127.0.0.1:8000/media/'

模型.py

from django.db import models

class Artist(models.Model):
    artistName = models.CharField(max_length = 30)
    artistInfo = models.TextField()

    def __unicode__(self):
         return self.artistName

class Album(models.Model):
    albumName = models.CharField(max_length = 30)
    artist = models.ForeignKey('Artist')
    date = models.DateTimeField('Release Date')
    albumInfo = models.TextField()
    albumArt = models.ImageField(upload_to="images/albumart/")

    def __unicode__(self):
        return self.albumName

class Song(models.Model):
    songName = models.CharField(max_length = 30)
    artist = models.ForeignKey('Artist')
    album = models.ForeignKey('Album')

    def __unicode__(self):
        return self.songName

base.html

<html>
<head>
    <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/Radio.css"/>
     {% block extended %}{% endblock %}
</head>
<body>
<div id="pageContainer">
    {% block content %}
    {% endblock %}
</div>
</body>
</html>

具体专辑.html

{% extends "base.html" %}
{% block content %}
   <div id="singlealbum">
       <p><img src="{{ songs.0.album.albumart.url }}"/>Name: {{ songs.0.album }}</p>
       <p>Artist:{{ songs.0.artist }}</a></p>
       <p>Song list:</p>
            {% for song in songs %}
               <p><a href="/songs/{{ song }}/">{{ song }}</a></p>
    {% endfor %}
   </div>
 {% endblock %}
4

1 回答 1

1

您的图像是否真的被上传到 Web 服务器期望为它们提供服务的地方?

根据您的MEDIA_URL设置,我猜您正在尝试让 Django 服务器提供图像。您可以查看文档以了解如何在开发期间执行此操作,或者如果您只是学习 Django,但请不要尝试为任何严重的事情执行此操作 - 使用专用 Web 服务器会更好地为您服务像阿帕奇之类的。

于 2013-06-11T18:12:45.950 回答