/1 参数的类型错误应该是字符串、字节或整数,而不是下载

米拉勋爵

我想从媒体文件中提供我的下载文件,并且正在从管理员上传文件。我尝试了一些东西,但出现此错误

TypeError at /1

argument should be string, bytes or integer, not Download

下面是我的代码。

查看.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

网址.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>

模型.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)
米拉勋爵

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

查看.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

它现在工作。用户现在可以下载文件

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型错误:参数应该是整数或类似字节的对象,而不是“str”

结果:失败异常:类型错误:参数应该是一个类似字节的对象或 ASCII 字符串,而不是“dict”

TypeError:listdir:路径应该是字符串,字节,os.PathLike或None,而不是DirectoryIterator

第 102 行:无法使用 'expr1'=input integer 调用 'operator =='。参数应该是类型:const string;

类型错误:参数应该是整数或无,而不是在 tkinter pyhton 中登录时出现“str”错误

socks.py 参数应该是整数或类似字节的对象,而不是“str”

错误:类型错误:参数 1 必须是字符串或 unicode 对象

类型错误:参数 1 必须是字符串或只读字符缓冲区,而不是文件(在 Learn python the Hard 的练习 17 中)

Error In = of (NIL 1) arguments 应该是 NUMBER 类型。共同的 Lisp

类型错误:DateTime :: __ construct()期望参数1为字符串,在laravel中给定的对象

烧瓶邮件类型错误:getaddrinfo() 参数 1 必须是字符串或无

参数1必须是字符串,而不是datetime.datetime?

难道“ a:1”不应该是python中的语法错误吗?

在Laravel雄辩的数据库播种机/工厂中播种关系数据-类型错误-Arg 1应该是Eloquent \ Model-给定集合

在 PHP 中 filter_id() 应该是整数还是字符串?

在Scala中,以字符串到案例类的映射以及以这些案例类作为输入参数的从字符串到函数的映射的类型应该是什么?

获取错误:org.openqa.selenium.WebDriverException:未知错误:键应该是字符串

BertTokenizer 错误 ValueError:输入 nan 无效。应该是字符串、字符串列表/元组或整数列表/元组

Terraform-变量应该是字符串类型,得到列表

laravel错误“ strtolower()期望参数1为字符串”?

PDO::query() 期望参数 1 是字符串错误

致命错误:未捕获的类型错误:传递给 DOMNode::appendChild() 的参数 1 必须是 DOMNode 的实例,字符串在

如何使用webpack2解决module.id应该是字符串错误

类型错误:utf_8_encode() 参数 1 必须是 str,而不是字节

-1%5在python中应该是什么结果

JavaScript 变量输出 1 但应该是 5

字符串常量类应该是静态的吗?

JavaScript typeof应该是数字,但返回字符串

大量的字符串应该是不可能的