我是 Django框架的新手,我试图生成一个从 Amazon S3 下载文件的链接。我尝试加载模板页面时收到此错误:
Reverse for 'myapp.views.handles3downloads' with arguments '(u'README.md',)'
and keyword arguments '{}' not found.
urls.py
urlpatterns = patterns('',
url(r'^handles3downloads/(\d+)/$', handles3downloads),
)
views.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>
我查看了一些类似错误的解决方案,但它对我没有帮助。任何人都可以帮帮我。
提前谢谢
答案 0 :(得分:1)
urls.py
文件中的正则表达式似乎有误。请尝试使用此代码:
url(r'^handles3downloads/(\w+)/$', handles3downloads),
您将参数字符串传递给视图,regex
匹配整数。
答案 1 :(得分:0)
乔丹是对的,你的urls.py有问题。你可以通过错误来判断。您试图在“myapp.views.handles3downloads”上获得反转,但是已经识别出反向字符串了吗?试试这个。
urlpatterns = patterns('',
url(r'^handles3downloads/([^/]+)/$', handles3downloads,
name='myapp.views.handles3downloads'),
)
答案 2 :(得分:0)
urls.py
url(r'^handles3downloads/', handles3downloads),
views.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>