1

我是Django 框架的新手,我试图生成一个链接以从Amazon S3下载文件。尝试加载模板页面时出现此错误:

Reverse for 'myapp.views.handles3downloads' with arguments '(u'README.md',)'
and keyword arguments '{}' not found.

网址.py

urlpatterns = patterns('',
    url(r'^handles3downloads/(\d+)/$', handles3downloads),
)

视图.py

def handles3downloads(request, fname):
    bucket_name = 'bucketname'
    key = s.get_bucket(bucket_name).get_key(fname)
    dfilename = key.get_contents_to_filename(fname)

    wrapper = HttpResponse(file(dfilename))
    response = HttpResponse(wrapper, content_type='text/plain')
    response['Content-Length'] = os.path.getsize(dfilename)
    return response

模板文件

<a href="{% url 'myapp.views.handles3downloads' sfile.linkUrl %}">{{sfile.linkUrl}}</a>

我查看了一些具有类似错误的解决方案,但对我没有帮助。谁能帮帮我。

提前谢谢

4

3 回答 3

2

您在文件中的正则表达式urls.py似乎是错误的。尝试改用这个:

url(r'^handles3downloads/(\w+)/$', handles3downloads),

您将参数字符串传递给视图,并且regex匹配整数。

于 2013-05-14T08:30:23.260 回答
0

Jordan 是正确的,你的 urls.py 有问题。你可以通过错误来判断。您正试图在“myapp.views.handles3downloads”上获得反向,但该反向字符串是否已被识别?试试这个。

urlpatterns = patterns('',
     url(r'^handles3downloads/([^/]+)/$', handles3downloads,
     name='myapp.views.handles3downloads'),
)
于 2013-05-16T19:43:00.320 回答
0

网址.py

url(r'^handles3downloads/', handles3downloads),

视图.py

def handles3downloads(request):
  fname = request.GET['filename']
  bucket_name = 'bucketname'
  key = s.get_bucket(bucket_name).get_key(fname)
  key.get_contents_to_filename('/tmp/'+key.name)
  wrapper = FileWrapper(open('/tmp/'+fname, 'rb'))
  content_type = mimetypes.guess_type('/tmp/'+fname)[0]
  response = HttpResponse(wrapper,content_type=content_type)
  response['Content-Length'] = os.path.getsize('/tmp/'+fname)
  response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(fname)

模板

<a href="/handles3downloads/?filename=file1.jpg" rel="external">Download</a>
于 2014-04-07T14:05:05.280 回答