3

我正在运行 localhost 来开发网站,Apache2 是我的 Web 服务器,我使用 Django 构建了该网站。我有兴趣将音轨添加到特定页面。我有另一个页面可以显示图像,我采用相同的方法来尝试播放音轨,但没有任何成功。URL 似乎是正确的,我需要为 FileFields 和 ImageFields 做些什么吗?我知道有一些用于播放音频的 jQuery 插件,但我是 Django 和 Web 开发的新手,所以我希望在继续使用更高级的方法之前让这个基本版本正常工作。

相关代码:

模型.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')
    audio_track = models.FileField(upload_to="songs/")

    def __unicode__(self):
        return self.songName

具体歌曲.html

{% extends "base.html" %}
{% block content %}
    <div id="singlesong">
        <p>Name: {{ song }}</p>
    <p>Artist: <a href="/artists/{{ song.artist }}/">{{ song.artist }}</a></p>
    <p>Album: <a href="/albums/{{ song.album }}/">{{ song.album }}</a></p>
    <p><audio controls="controls">
        <source src="{{ song.audio_track.url }}" type="audio/mpeg" />
    </audio>
</div>
{% endblock %}

视图.py

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

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

网址.py

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    (r'^songs/$', 'Radio.views.SongsAll'),
    (r'^songs/(?P<songname>.*)/$', 'Radio.views.SpecificSong'),
    (r'^artists/(?P<artistname>.*)/$', 'Radio.views.SpecificArtist'),
    (r'^albums/(?P<albumname>.*)/$', 'Radio.views.SpecificAlbum'),
    (r'^register/$', 'listener.views.ListenerRegistration'),
    (r'^login/$', 'listener.views.LoginRequest'),
    (r'^logout/$', 'listener.views.LogoutRequest'), 
    (r'^resetpassword/passwordsent/$', 'django.contrib.auth.views.password_reset_done'),
    (r'^resetpassword/$', 'django.contrib.auth.views.password_reset'),
    (r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'),
    (r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
    (r'^profile/$', 'listener.views.Profile'),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py(缩写)

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

1 回答 1

1

对于将来遇到此问题的任何人,以下是可能解决您的问题的步骤列表。

  1. 确保您使用的浏览器支持您使用的音频文件类型。(应该很明显,但我犯了这个错误)
  2. 确保权限是上传到您的设置/从您的设置上传的权限。
  3. 确认文件已正确上传到您的媒体文件夹。
  4. 重新启动您的服务器。

这些步骤对我有所帮助,希望对其他人有所帮助。

于 2013-07-23T17:37:37.947 回答