问题描述
我有一个内联表单来上传一些文件.当我这样做时,我得到了这些网址:
I have an inline form to upload some files. When I do it, I get these urls :
localhost/files/my-photo.jpg
localhost/files/my-pdf.pdf
但是,当我单击这些链接时,出现一个问题,告诉我该URL不匹配.我不知道如何配置 urls.py
.我需要一个视图吗?TY!
However, when I click on these link, an issue say me that the url is not match. I don't know how to configure urls.py
. Do I need a view ? TY !
推荐答案
Django的文档在此主题上令人困惑.在管理静态文件"文章中而不是在文件上传"中讨论了上传文件的URL.实际将上传的文件作为简单的静态文件:
Django's docs are surprisingly confusing on this topic. Uploaded file's URLs are discussed in "Managing static files" article, not in "File uploads". Uploaded files actually are served as simple static files:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
还要确保仅在开发环境中使用此功能.正如@Wolkodav所说,您应该配置Web服务器以在生产环境中提供上传服务.相关链接:
Also make sure you use this feature in development environment only. As @Wolkodav said, you should configure your web server to serve uploads in production. Related links:
https://docs.djangoproject.com/zh/1.8/ref/settings/#media-url
这篇关于Django直接链接到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!