I want to convert the file path to url in django project. How can I convert the file field into url?
Asked
Active
Viewed 7,382 times
4
-
1There's not nearly enough detail here. What file path? What URL do you want to end up with? What relationship would a file path have with a URL? – Daniel Roseman Apr 06 '16 at 09:27
1 Answers
5
Without further configuration, files stored via Django's models.FileField
(also models.ImageField
) end up in your MEDIA_ROOT
folder and will be available under the base path defined by MEDIA_URL
using the folder structure under MEDIA_ROOT
.
Have a look at:
- https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.FileField
- https://docs.djangoproject.com/en/2.2/topics/http/file-uploads/
- https://docs.djangoproject.com/en/2.2/ref/settings/#media-root
- https://docs.djangoproject.com/en/2.2/ref/settings/#media-url
- Django MEDIA_URL and MEDIA_ROOT
- Django Rest get file from FileField url
When the configuration is correct, in your template the link to the file is
<a href="{{ object.file.url }}">Download File</a>
If this question is about static files, search for static
and staticfiles
in the Django documentation. It is similar to the media configuration.
EDIT: updated links

Risadinha
- 16,058
- 2
- 88
- 91