如何播放已上传到python的html中的mp3?

时间:2016-04-07 00:21:36

标签: python html django

我刚刚开始使用python / django,我想知道是否有任何方法可以播放上传的mp3?我可以打开一个上传的docx或txt文件但是有了mp3并播放它们,我得到“Not Found:/ list / 03 The Forgotten.mp3”,03 The Forgotten.mp3是一首随机歌曲我已经上传。

这是我的views.py

from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from docx import Document
from polls.models import Document
from polls.forms import DocumentForm
from django.http import HttpResponse



import docx2txt
def list(request):
    # Handle file upload
    context = {}
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc =  request.FILES['docfile']
            #newdoc.save()
            try:
                name = newdoc.name
                if name.endswith("docx"):
                    text = docx2txt.process(newdoc)
                    context["uploadedFile"] = text
                elif name.endswith("txt"):
                    context["uploadedFile"] = newdoc.read()
                elif name.endswith("mp3"):
                    context["sound"] = newdoc
                    context["uploadedFile"] = "enjoy!"


            # Redirect to the document list after POST
            #return HttpResponseRedirect(reverse('polls.views.list'))
            #context["uploadedFile"] = newdoc.read()
            except:
                context["uploadedFile"] = "not a word doc"





            return render_to_response('fileUploadPage.html', context)
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()


    # Render list page with the documents and the form
    return render_to_response(
        'polls/list.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )

和我的html页面,list.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Minimal Django File Upload Example</title>
    </head>
    <body>
    <!-- List of uploaded documents -->
    {% if documents %}
        <ul>
        {% for document in documents %}
            <li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a></li>

        {% endfor %}
        </ul>
    {% else %}
        <p>No documents.</p>
    {% endif %}

        <!-- Upload form. Note enctype attribute! -->
        <form action="{% url 'list' %}" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            <p>{{ form.non_field_errors }}</p>
            <p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
            <p>
                {{ form.docfile.errors }}
                {{ form.docfile }}
            </p>
            <p><input type="submit" value="Upload" /></p>
        </form>
    </body>
</html>

最后是我的fileUploadPage.html

<div id="uploaded">
    {{ uploadedFile }}

    <audio id="Player" autoplay>
     <source src= "{{sound}}" />
    </audio>
</div>

我的fileUploadPage位于我的apps文件夹(templates / myapp /)中,而不是模板(templates /)

0 个答案:

没有答案