Thomas Saied
Thomas Saied

Reputation: 202

how i do to download file from my website?

When I define FileField in django this field to upload the files I need when uploading on my site, I also want to put the possibility of downloading these files again from my site how can I write a code to download the files again. I want to work as a site to upload and download files

Upvotes: 0

Views: 83

Answers (3)

Adam
Adam

Reputation: 469

There are multiple ways to do that. First is to pass your model through views and reference the files in template as {{mymodel.filefield.url}}, you can also use loop or if statements or whatever is your method of display in template.

Other method, less automated is to hardcode it and write HTML tag with path to the file. To manage your static files in your project please see:

https://docs.djangoproject.com/en/2.1/howto/static-files/

You can also use file manager and custom FileFields and package described methods:

https://djangopackages.org/grids/g/file-managers/

Hope this helps.

Upvotes: 1

Carlos Herrera
Carlos Herrera

Reputation: 143

In your templates, you can do this:

{% for f in files %}
<a href="{{ f.file.url }}">Download file</a>
{% endfor %}

Upvotes: 1

stealthflyr
stealthflyr

Reputation: 81

You would just need to link that file somewhere on your template.

If your files upload to 'example.site/static/file.txt'

Then in your template make an anchor tag refer to that destination

<a href="/static/file.txt" download>Download</a>

Upvotes: 0

Related Questions