【问题标题】:how to download a file in django如何在 django 中下载文件
【发布时间】:2018-01-24 14:08:00
【问题描述】:

我上传了一些存储在 /media/documents 中的文件。我还在 settings.py 中设置了 MEDIA_URL 和 MEDIA_ROOT,并在 urls.py 中给出了适当的 url。 文件是通过表单上传的,现在我需要下载上传的文件。 我对如何从浏览器下载感到困惑。有人可以帮助我吗,我是 django 的新手。

型号

class Document(models.Model):
    description = models.CharField(max_length=255, blank=False)
    document = models.FileField(upload_to='documents/')
    uploaded_at = models.DateTimeField(auto_now_add=True)
    is_diagnoised = models.BooleanField(default=False)
    uploaded_by = models.CharField(max_length=20, default="")
    downloaded_by = models.CharField(max_length=20, default="")`

views.py

def index(request):
if request.user.is_authenticated:
    uid = request.user.id
    usern = request.user.username
    if Profile.objects.filter(role='Doctor', id=uid):#file upload
        if request.method == 'POST':
            form = DocumentForm(request.POST, request.FILES)
            if form.is_valid():
                post = form.save(commit=False)
                post.uploaded_by = usern
                post.save()
                return redirect('index')
        else:
            form = DocumentForm()
            no_diagno = Document.objects.filter(is_diagnoised=False, uploaded_by=usern)
            is_diagno = Document.objects.filter(is_diagnoised=True, uploaded_by=usern)
            context = {
                'ndg':no_diagno,
                'ydg':is_diagno,
                'form': form
            }
        return render(request, 'upload.html', context)

    else:#file download
        if request.method == 'POST':
            form = RadioForm(request.POST, request.FILES)
            if form.is_valid():
                post = form.save(commit=False)
                post.downloaded_by = usern
                post.is_diagnoised = 'True'
                post.save()
                return redirect('index')
        if request.method == 'GET':
            form = RadioForm()
            no_diagno = Document.objects.filter(is_diagnoised=False)
            is_diagno = Document.objects.filter(is_diagnoised=True)
            context = {
                'ndg':no_diagno,
                'ydg':is_diagno,
                'form': form
            }
        return render(request, 'index-rad.html', context)

def download(request, path):
    file_path = os.path.join(settings.MEDIA_ROOT, media/documents)
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read())
            response['Content-Disposition'] = 'inline; filename=' +os.path.basename(file_path)
            return response
    raise Http404

index-rad.html

{% for n in ndg %}
<ol>
<li>{{n.description}} <a href="{% url 'download' %}">download</a></li>
</ol>
{% endfor %}

【问题讨论】:

  • 请参考 this 链接以了解更多关于在 SO 上提问的信息,因为您的问题不是很具描述性或不包括您的努力或示例。
  • 您的问题到底是什么?你真的不需要让 django 处理文件服务。这可以通过 nginx 之类的 Web 服务器来完成。
  • 您的download 视图包含两个未定义变量media 除以documents。那应该是一个字符串吗?而且您永远不会使用提供的 path 参数。这很混乱。

标签: django django-models django-templates django-views


【解决方案1】:

问题不是很丰富,但我不明白,您需要在您的 teplate 中找到文件的 url。为此,您可以简单地使用 {{ myfile.url }}。

【讨论】:

  • 我已经更新了我的问题。你能帮助我吗。我卡在这一点上。
猜你喜欢
  • 2020-01-10
  • 1970-01-01
  • 2017-03-13
  • 1970-01-01
  • 2013-04-06
  • 2021-02-16
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
相关资源
最近更新 更多