0

I have a model form with

file1=forms.FileFiled(required=False)

the template shows it fine, with the default widget when using {{ f.file1 }} if the form has data from an instance, how can I know the file name and url in the template?

I would like to serve the file myself with a function instead of how django shows it in the template, which does not work.

karthikr
  • 97,368
  • 26
  • 197
  • 188

1 Answers1

0

You can access to the relative path doing:

{{ f.file1.name }}

But don't forget to add in your setting.py:

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

You can also check the official Django documentation: FileField

eduzen
  • 131
  • 6
  • Thanks, I added the settings details and now file1.name gives me the name of the file now, but not the url. Is it because the path is not in the media folder? these are doc and pdf files. in djangoprojectfolder/files/fileX – George Battaglia Aug 03 '17 at 21:22