I have a Django project in a directory called 'planner'. I would like all uploaded csv files to be saved in 'planner/uploads' as I would like to keep the Django projects separated. Currently if I upload csv files they're automatically saved to '~/uploads' and I get an error saying that the file could not be found, but the outputted filename is correct.
As a workaround I'm testing to see if the app is in debug mode, and if it isn't then I'm adjusting the filename to load files in the '~/uploads' directory. What am I doing wrong? I have attached the offending code below:
if request.method == 'POST':
file_path = request.FILES.get('csv_upload')
upload = Upload.objects.create(file=file_path)
relative_path = upload.file.url
base_url = str(settings.BASE_DIR)
absolute_path = base_url + relative_path
if settings.DEBUG:
try:
with open(absolute_path, 'r') as file:
for line in file:
fields = line.split(',')
loader = Loader.objects.get(name=fields[0])
Shift.objects.update_or_create(
loader = loader,
name = fields[1],
start = fields[2],
finish = fields[3]
)
messages.success(request, 'Data imported successfully.')
except Loader.DoesNotExist:
messages.error(request, f'Loader \'{fields[0]}\' does not exist.')
except Exception as e:
messages.error(request, str(e))
if not settings.DEBUG:
try:
with open('/'.join(['/home/<username>/', relative_path]), 'r') as file:
for line in file:
fields = line.split(',')
loader = Loader.objects.get(name=fields[0])
Shift.objects.update_or_create(
loader = loader,
name = fields[1],
start = fields[2],
finish = fields[3]
)
messages.success(request, 'Data imported successfully.')
except Loader.DoesNotExist:
messages.error(request, f'Loader \'{fields[0]}\' does not exist.')
except Exception as e:
messages.error(request, str(e))