To load the files, the FileField class is used.
FileField class (upload_to = None, max_length = 100, ** options)
The directory where the files are saved must be specified.
Example:
class MyModel(models.Model):
# file will be uploaded to MEDIA_ROOT/uploads
upload = models.FileField(upload_to='uploads/')
# or...
# file will be saved to MEDIA_ROOT/uploads/2015/01/30
upload = models.FileField(upload_to='uploads/%Y/%m/%d/')
You must also have configured the "settings.py" file with the file addresses.
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
Storage can also be done remotely, that is, have another storage server, be a server and make the connection through FTP or use another service such as AWS S3.
if used FTP:
DEFAULT_FILE_STORAGE = 'storages.backends.ftp.FTPStorage'
USER = 'userftp'
PASSWORD = "passftp"
HOST = 'ftp.host.com or IP'
PORT = 21
FTP_STORAGE_LOCATION = "ftp://{user}:{passwd}@{host}:{port}/".format(user=USER, passwd=PASSWORD, host=HOST, port=PORT)
if used S3
AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_BUCKET_NAME')
AWS_ACCESS_KEY_ID = os.getenv('AWS_S3_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY_ID = os.getenv('AWS_S3_SECRET_KEY')
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = { 'CacheControl': 'max-age=86400' }
AWS_LOCATION = 'static'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'code/static'),]
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
and in the "urls.py" file add:
urlpatterns = [
url(r'^admin/', admin.site.urls),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)