Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert file path to url in django

I want to convert the file path to url in django project. How can I convert the file field into url?

like image 689
naveen Avatar asked Sep 03 '25 02:09

naveen


1 Answers

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

like image 54
Risadinha Avatar answered Sep 07 '25 16:09

Risadinha