简体   繁体   English

上传文件 django 管理员

[英]Upload file django admin

I'm creating a admin form where users can upload files.我正在创建一个管理表单,用户可以在其中上传文件。 I want this to be stored in a secure place where the public can't get it.我希望将其存储在公众无法获取的安全地方。 I'm using a standard form with a FileField and no model.我正在使用带有FileField且没有 model 的标准表单。 Is there a way I can store the files in a folder within the app folder?有没有办法可以将文件存储在 app 文件夹内的文件夹中?

Yes you can do that by specifying the path in your settings.py file eg.是的,您可以通过在 settings.py 文件中指定路径来做到这一点,例如。 MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'

Yes you can do this是的,你可以这样做
Add these line in your project level file urls.py(which is auto generated when project is created)在您的项目级文件 urls.py 中添加这些行(创建项目时自动生成)

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

And add these lines in your settings.py并将这些行添加到您的 settings.py

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM