/ 1参数的TypeError应该是字符串,字节或整数,而不是下载

时间:2017-05-27 08:31:49

标签: python django

我想从媒体文件提供下载文件,文件正从管理员上传。我试了一下,我收到了这个错误

TypeError at /1

argument should be string, bytes or integer, not Download

以下是我的代码。

view.py

    def download(request, download_id):
        downloading = Download.objects.get(pk=download_id)
        if os.path.exists(downloading):
        with open(downloading, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="text/pdf")
            response['Content-Disposition'] = 'inline; filename=' + os.path.basename(downloading)
            response['Content-Length'] = os.path.getsize(downloading)
            return response
        pdf.closed
    raise Http404

url.py

    url(r'^(?P<download_id>\d+)$', views.download, name='download'),

html链接

    <a href="{% url 'peruse:download' download.id %}" class="btn btn-generic btn-sm" role="button">DOWNLOAD</a>

model.py

    class Download(models.Model):
        pdf_name = models.CharField(max_length=500, blank=False)
        pdf_file = models.FileField(upload_to='Downloads/%d-%m-%Y/', blank = False,)
        created_at = models.DateTimeField(auto_now_add = True)
        updated_at = models.DateTimeField(auto_now = True)
        is_visible = models.BooleanField(default = True)

        def __str__(self):
            return self.pdf_name

此视图处理上传。

    def upload(request):
        uploading = Download.objects.filter(is_visible = True)
        context = { 'uploading' : uploading }
        fillAuthContext(request, context)
        return render(request, 'library/resources.html', context)

2 个答案:

答案 0 :(得分:2)

您必须将参数作为download file path字符串而不是Download对象传递,只需将download函数更改为:

def download(request, download_id):
    downloading = Download.objects.get(pk=download_id)
    file_path = downloading.pdf_file.name
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="text/pdf")
            response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
            response['Content-Length'] = os.path.getsize(file_path)
            return response
        pdf.closed
    raise Http404

我已经验证了您的代码,这对您有用:

http://127.0.0.1:8000/peruse/download/1download_id=1此处)将为您提供第一个文件:

enter image description here

<强>更新

确保您的根yourproject/urls.py是这样的:

from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r'^peruse/',include('peruse.urls')),
    url(r'^admin/', admin.site.urls),
]

yourproject/peruse/urls.py就像这样:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^download/(?P<download_id>\d+)$', views.download, name='download'),
]

并确保您已上传文件。

答案 1 :(得分:0)

谢谢大家的帮助。我能够想出一些符合你想法的东西。

view.py

    def download(request, download_id):
        downloading = Download.objects.get(pk=download_id)
        path = downloading.pdf_file.name
        file_path = os.path.join(settings.MEDIA_ROOT, path)
        if os.path.exists(file_path):
            with open(file_path, 'rb') as pdf:
                response = HttpResponse(pdf.read(), content_type="text/pdf")
                response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
                response['Content-Length'] = os.path.getsize(file_path)
                return response
            pdf.closed
        raise Http404

现在正在运作。用户现在可以下载文件