【问题标题】:TypeError at /1 argument should be string, bytes or integer, not Download/1 参数处的 TypeError 应该是字符串、字节或整数,而不是下载
【发布时间】:2017-05-27 08:31:49
【问题描述】:

我想从媒体文件中提供我的下载文件,并且这些文件正在通过管理员上传。我尝试了一些东西,但我收到了这个错误

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)

【问题讨论】:

  • 请格式化您的代码,整篇文章真的很难阅读
  • 格式化谢谢
  • 正在尝试使用我在这里看到的 stackoverflow.com/questions/36392510/django-download-a-file。请随时纠正您发现的任何错误。使用 django 不太好。谢谢
  • 错误发生在哪一行?是open 吗?请发布您的整个错误消息和回溯。 Download 来自哪里?
  • downloading 应该是您的 download file path 字符串,而不是 Download 对象。

标签: python django


【解决方案1】:

您必须将参数作为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/1(此处为download_id=1)会给你第一个文件:

更新:

确保您的根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'),
]

并确保您已经上传了文件。

【讨论】:

  • 非常感谢,但它会引发错误;找不到页面 (404) 请求方法:GET 请求 URL:127.0.0.1:8000/1 提出者:peruse.views.download
  • 我认为您的urls 有问题,请查看我更新的答案。
【解决方案2】:

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

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

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

【讨论】:

  • 你的想法和我的回答完全一样,只是我不确定你的Download文件夹在你的MDEIA_ROOT中,我以为它在你的项目根文件夹中。
  • 是的。一样的。这就是我如何正确引导它的想法。非常感谢
  • 如果它有帮助,并且是正确的解决方案,则将其标记为接受答案,人们会花费数小时来解决您的问题,帮助指导正确的方式,请尊重他们的时间和精力放。 :)
  • 耶。我在贴的那天试着加厚它。但是直到两天后我才被允许这样做。但它现在完成了。谢谢
  • 嗨 Tiny D。我可以亲自问你一些关于 django 的问题。我的邮箱是lord.mila@yahoo.com 谢谢
猜你喜欢
  • 2020-09-25
  • 2021-05-04
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-18
相关资源
最近更新 更多