我正在运行localhost来开发一个网站,Apache2是我的网络服务器,我用Django构建了这个网站。我有兴趣将音轨添加到特定页面。我有另一个页面,我会显示图像,我采取相同的方法试图让音轨播放,但没有取得任何成功。 URL看起来是正确的,我需要为FileFields和ImageFields做些什么。我知道有一些用于播放音频的jQuery插件,但我是Django和Web开发的新手,所以我希望在继续使用更高级的方法之前让这个基本版本正常工作。
相关代码:
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')
audio_track = models.FileField(upload_to="songs/")
def __unicode__(self):
return self.songName
specificsong.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 %}
views.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))
urls.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/'
答案 0 :(得分:1)
对于将来遇到此问题的任何人,此处列出了可能解决您问题的步骤。
这些步骤对我有所帮助,希望它能帮助别人。