django-jinja href未下载文件

时间:2019-02-28 10:17:32

标签: django django-models django-templates

伙计们,我正在制作一个页面,人们可以下载我的东西。我制作了一个thta模型,并且上传成功,但是当我单击下载按钮前端的问题时,我没有下载那个东西。相反,它会下载页面的副本。

在这里,是models.py

class Upload(models.Model):
    image = models.ImageField(upload_to = 'images',)
    file = models.FileField(upload_to = 'images/%Y/%M/%d/')
    name = models.CharField(max_length = 200)

    def __str__(self):
        return self.name

这里是views.py

def upload(request):
    upload = Upload()
    return render(request,'app/download.html',{'upload':upload})

这是html文件

{% block content %}
<div class="container">
    <div class="download">
        <p style="text-align: center;">
        <img src="{{upload.image}}" alt="Image containing link to you,r success">
        </p>
    </div>

    <h2>Click on the button below</h2>
    <button class="btn btn-primary"><a href="{{upload.file.id}}" download>Yeah do it</a></button>
</div>





{% endblock %}

1 个答案:

答案 0 :(得分:0)

尝试

{% block content %}
<div class="container">
    <div class="download">
        <p style="text-align: center;">
        <img src="{{upload.image}}" alt="Image containing link to you,r success">
        </p>
    </div>

    <h2>Click on the button below</h2>
    <a href="{{upload.file.url}}" class="btn btn-primary">Yeah do it</a>
</div>
{% endblock %}

更改说明:

  • {{upload.file.url}}将为您提供上传文件的网址。
  • 直接将"btn btn-primary"添加到您的定位标记。它将显示为按钮。您不需要单独的<button>标签