开发者

Django admin shows full path to uploaded file, and link contains full path. How can I fix this?

I have a model which is defined like this:

class Attachment(models.Model):
  file            = models.FileField(upload_to=MEDIA_ROOT)

MEDIA_ROOT is defined 开发者_JS百科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 of MEDIA_ROOT. From the docs, upload_to should be:

A local filesystem path that will be appended to your MEDIA_ROOT setting to determine the value of the url attribute.

In other words, the path that you specify in upload_to will be appended to MEDIA_ROOT to form the upload directory, and appended to MEDIA_URL to 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 do upload_to='uploads/%Y/%m/'. See docs for more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜