
Django 프로젝트에서 file_uploads를 설정할 때, 업로드 한 파일의 이름이 중복되면 기존 파일을 덮어씌우는 현상이 발생하는 것을 해결하기 위해서는 `settings.py` 파일에서 `MEDIA_FILE` 및 `MEDIA_URL`을 설정하는 것이 중요합니다.
`MEDIA_FILE`은 업로드 한 파일을 저장할 디렉토리를 지정하고, `MEDIA_URL`은 업로드 한 파일의 URL을 지정합니다.
#hostingforum.kr
python
# settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
그리고 `settings.py` 파일의 `FILE_UPLOAD_MAX_MEMORY_SIZE`를 설정하여 업로드 한 파일의 크기를 제한할 수 있습니다.
#hostingforum.kr
python
# settings.py
FILE_UPLOAD_MAX_MEMORY_SIZE = 1024 * 1024 * 10 # 10MB
또한, `settings.py` 파일의 `FILE_UPLOAD_TEMP_DIR`를 설정하여 업로드 한 파일을 임시로 저장할 디렉토리를 지정할 수 있습니다.
#hostingforum.kr
python
# settings.py
FILE_UPLOAD_TEMP_DIR = os.path.join(BASE_DIR, 'temp')
이러한 설정을 통해 업로드 한 파일의 이름이 중복되더라도 기존 파일을 덮어씌우는 현상이 발생하지 않도록 할 수 있습니다.
또한, Django의 `django.core.files.storage` 모듈을 사용하여 업로드 한 파일을 관리할 수 있습니다. 예를 들어, `django.core.files.storage.FileSystemStorage` 클래스를 사용하여 업로드 한 파일을 파일 시스템에 저장할 수 있습니다.
#hostingforum.kr
python
# models.py
from django.core.files.storage import FileSystemStorage
fs = FileSystemStorage(location='/path/to/storage', base_url='/path/to/storage/')
class MyModel(models.Model):
file = models.FileField(upload_to='uploads/', storage=fs)
이러한 방법을 통해 업로드 한 파일을 관리할 수 있습니다.
2025-06-28 10:31