Python 从django 1.4.3中的媒体下载文件

Python 从django 1.4.3中的媒体下载文件,python,django,file,download,media,Python,Django,File,Download,Media,我正在使用django设计两个基本页面,其中一个页面用于将文件上载到媒体,另一个页面列出媒体文件夹中所有上载的文件以及下载这些文件的链接。下面是我的代码 url.py from django.conf.urls.defaults import * from django.conf import settings urlpatterns = patterns('', url(r'^files$', 'learn_django.views.upload_file'),

我正在使用django设计两个基本页面,其中一个页面用于将文件上载到媒体,另一个页面列出媒体文件夹中所有上载的文件以及下载这些文件的链接。下面是我的代码

url.py

from django.conf.urls.defaults import *
from django.conf import settings

urlpatterns = patterns('',
             url(r'^files$', 'learn_django.views.upload_file'),
             url(r'^list_of_files$', 'learn_django.views.files_list'),
             url(r'^download$', 'learn_django.views.download'),
)
if settings.DEBUG:
    urlpatterns = patterns('',
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + urlpatterns
from django.conf import settings
from django.shortcuts import render_to_response
from learn_django.forms import UploadFileForm
import os

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid() and form.is_multipart():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/files_list')
    else:
        form = UploadFileForm()
    return render_to_response('files_form.html', {'form': form},context_instance=RequestContext(request))

def handle_uploaded_file(file,path=''):
    filename = file._get_name()
    destination_file = open('%s/%s' % (settings.MEDIA_ROOT, str(path) + str(filename)), 'wb+')
    for chunk in file.chunks():
        destination_file.write(chunk)
    destination_file.close()

def files_list(request):
    return render_to_response('files_list.html',{'total_files':os.listdir(settings.MEDIA_ROOT),'path':settings.MEDIA_ROOT},context_instance=RequestContext(request))

def download(request):
    #do something to downlaod the files here.....
    return something
文件\u list.html

<table border="1" colspan="2" width="100%">
   <tr>
     <th width="60%">File</td>
     <th width="40%">Download</td> 
   </tr>
 {% for file in total_files %}
   <tr>
     <td width="60%">{{file}}</td>
     <td width="40%" align="center"><a href="/download" style="text-decoration:None">Download here</a></td> 
   </tr>
 {% endfor %}  
</table>
编辑下载的查看功能,如下所示

def download(request,file_name):
    response = HttpResponse(mimetype='application/force-download')
    response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
    response['X-Sendfile'] = smart_str(settings.MEDIA_ROOT + file_name)
    return response
html文件中的锚定标记如下所示

<td width="40%" align="center"><a href="/download/{{file}}" style="text-decoration:None">Download here</a></td>
url(r'^download/$,'learn_django.views.download'),
def下载(请求):
file\u name=request.GET.GET('per\u page')
路径到文件=“/media/{0}”。格式(文件名)
response=HttpResponse(mimetype='application/force download')
响应['Content-Disposition']='附件;文件名=%s“%smart\u str(文件名)
响应['X-Sendfile']=smart\u str(路径到文件)
返回响应

:我们如何将文件名和路径从锚定标记中的模板文件\u list.html发送到\u文件?k我尝试了相同的方法,文件正在下载,但当我打开它时,文件中没有任何内容,并显示一些错误显示以匹配url中的模式?我尝试了类似url(r“^download/*)$”、“learn_django.views.download”)的方法,然后出现了以下错误异常值:不平衡括号异常位置:/usr/lib64/python2.7/re.py在_compile,第245行,那么如何将url中的文件名与re模式匹配?
<td width="40%" align="center"><a href="/download/{{file}}" style="text-decoration:None">Download here</a></td>
Request Method: GET
Request URL:    http://localhost:8000/download
Django Version: 1.4.3
Exception Type: error
Exception Value:    
unbalanced parenthesis
Exception Location: /usr/lib64/python2.7/re.py in _compile, line 245
Python Executable:  /usr/bin/python
url(r'^download/$', 'learn_django.views.download'),

<a href="/download/?file_name={{file}}">Download</a>

def download(request):
    file_name = request.GET.get('per_page')
    path_to_file = "/media/{0}".format(file_name)
    response = HttpResponse(mimetype='application/force-download')
    response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
    response['X-Sendfile'] = smart_str(path_to_file)
    return response