I have a model which is defined like this:
class Attachment(models.Model):
file = models.FileField(upload_to=MEDIA_ROOT)
MEDIA_ROOT is defined using it’s absolute path, and it’s something like d:\django\my_proj\media . In the admin, the link to it appears like this: http://localhost:8000/media/d:/django/my_proj/media/file.txt . How can I fix this?
Use
/instead ofMEDIA_ROOT. From the docs,upload_toshould be:In other words, the path that you specify in
upload_towill be appended toMEDIA_ROOTto form the upload directory, and appended toMEDIA_URLto form the URL to the file.p.s. it might be worth specifying a subdirectory instead of
/just so the uploaded files cannot overwrite your media files. For example,upload_to='uploads/'. To organise you uploads by year/month, you can even doupload_to='uploads/%Y/%m/'. See docs for more details.