Django中的ImageField

时间:2013-06-11 17:57:39

标签: python django django-templates django-admin django-views

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

views.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/'

models.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>

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

1 个答案:

答案 0 :(得分:1)

您的图片实际上是在Web服务器希望为其提供服务的地方上传的吗?

从你的MEDIA_URL设置我猜你正在尝试让Django服务器为图像提供服务。您可以在开发期间查看documentation如何执行此操作,或者如果您只是在学习Django,但请不要尝试对任何严重的事情执行此操作 - 使用它会更好像Apache这样的专用Web服务器。