繁体   English   中英

如何使用 django 创建下载链接?

[英]How can i create download link using django?

我正在使用 django 使用我自己的项目。我正在尝试使用 django 下载文件。我已完成使用 django 上传文件。但我不知道如何在 django 中创建文件下载链接。示例)test.java,以及我的域是 example.com,端口是 9001,媒体文件夹是 /media 我只想像这样关闭https://example.com:9001/media/test.java 我用谷歌搜索了所有方法,但没有任何线索。这是我的代码。 view.py -> 上传部分

@csrf_exempt
def index(request):
    return render(request, 'useraccount/index.html', {})

@csrf_exempt
def file_list(request):
    return render(request, 'useraccount/list.html', {})

@csrf_exempt
def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('file_list')

    else:
        form = UploadFileForm()
    return render(request, 'useraccount/upload.html', {'form': form})

上传.html

<html>
    <head><title>Upload Test</title></head>
    <body>
        <form action="upload/"
              method="post"
              enctype="multipart/form-data">
            File:
            <input type="file"
                   name="file"
                   id="id_file" />
            <input type="submit" value="UPLOAD" />
        </form>
    </body>
</html>

上传.html

{% extends 'useraccount/index.html' %}

{%  block content %}
    <h2>Upload file</h2>
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form }}
        <button type="submit">Upload file</button>
    </form>
{% endblock %}

列表.html

{% extends 'useraccount/index.html' %}

{% block content %}
    <h2>The image has been uploaded!!</h2>

{% endblock %}

forms.py

from django import forms
from .models import UploadFileModel

class UploadFileForm(forms.ModelForm):
    class Meta:
        model = UploadFileModel
        fields = {'title', 'file'}

url.py

from django.urls import path, include
from django.conf.urls import url
from . import views
from django.conf import settings
from django.conf.urls.static import static

  path('upload/', views.upload_file, name='upload_file'),
    path('list/', views.file_list, name='file_list'),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

您可以通过url方法生成url文件,如下所示:

在.py:

my_file_field.url

在模板中:

<a href="{% my_file_field.url %}"> file link ! </a>

请参阅 Django 的文档https://docs.djangoproject.com/en/dev/topics/files/

要下载存储在数据库中的图像,它将是:

简单例子

视图.py

def download_image(self, imageid):
    image = UploadFileModel.objects.get(pk=imageid)
    image_buffer = open(image.file.path, "rb").read()
    content_type = magic.from_buffer(image_buffer, mime=True)
    response = HttpResponse(image_buffer, content_type=content_type);
    response['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(image.file.path)
    return response

网址.py

path('downloadimage/<str:imageid>/$', views.download_image, name='download_image'),

模板.html

<a href="{% url 'useraccount:download_image' imageid=<id_of_image> %}" type="button">Download image</a>

注意:请将模板中的<id_of_image>替换为所需的图像 ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM