0

I have a Django app that I just added to the already deployed Django web on Apache. Because it is ran by Apache, path of the media folder seems to be different.

My app lets the user upload an excel file which then changes numbers and save as csv file.

(only showed relevant folders/code snippets) Current directory

converts\
    _init_.py
    apps.py
    forms.py
    models.py
    converter.py
    urls.py
    views.py
main\
    settings.py
    urls.py
    wsgi.py
meida\
    excels\
        example.xlsx
    csvs\
        example.csv
static\
manage.py

settings.py

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

main\urls.py

urlpatterns = [
    path('', RedirectView.as_view(url='/converts/')),
    path('converts/', include('converts.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The part that causes the problem is the following in converts/converter.py:

def convertExcel(name):
    path = 'media/excels/'
    key = path + name
    wb = load_workbook(key)

Originally in development, a function in view calls convertExcel(example.xlsx) and the workbook, via media/excels/example.xlsx, finds the correct file to work with the loaded workbook. But in production server, it gives

FileNotFoundError: [Errno 2] No such file or directory: 'media/excels/example.xlsx'

My Question is: Do I have to back track from where apache2.conf is to find the path? Or is there a way to set path to my django project so i can just set path in my convertExcel() as 'media/excels'? Or is there any other way I can call the uploaded workbook?

Any kind of help/comment would be appreciated. Please comment if additional information is needed.

John Gear
  • 21
  • 4

1 Answers1

0

My guess is that you should use MEDIA_ROOT variable because it points to the uploaded files. So you would have

def convertExcel(name):
    from django.conf import settings
    path = os.path.join(settings.MEDIA_ROOT, 'excels')
    key = os.path.join(path, name)
    wb = load_workbook(key)
marke
  • 1,024
  • 7
  • 20
  • I receive exact same error after running it :( Mo such file or directory 'media/excels/example.xlsx' – John Gear May 02 '19 at 22:22
  • Does apache point to this directory? Is the file in MEDIA_ROOT after the uploading? – marke May 02 '19 at 22:26
  • How do I know whether it points to this directory? Considering the directory path it states it cannot find, starts at media, i'm guessing it isn't pointing correctly. – John Gear May 02 '19 at 22:31
  • Yeah, you have to configure apache, for example: https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/modwsgi/#serving-files – marke May 02 '19 at 22:42
  • "Alias /media/ /path/to/mysite.com/media/" should become "Alias " – marke May 02 '19 at 22:46
  • I have seen that page but I was confused with exactly what file I need to modify. What file am I suppose to edit? And thank you for continuous help! – John Gear May 02 '19 at 22:53
  • no problem :) here is how to find apache config file https://stackoverflow.com/questions/12202021/lost-httpd-conf-file-located-apache – marke May 02 '19 at 23:01
  • Okay. In apache2.conf, I have added Alias Require all granted What else do I need to add? – John Gear May 02 '19 at 23:12
  • and are just placeholders for the real values, for the real paths – marke May 02 '19 at 23:26
  • I have tried with Alias /media/ /home/test/projects/project1/media Require all granted in apache2.conf and it still shows same error :( – John Gear May 02 '19 at 23:40
  • Could you print MEDIA_ROOT and make sure it is exactly same as '/home/test/projects/project1/media '? Also are you sure you have craeted this directory on the machine that runs apache? – marke May 03 '19 at 00:00
  • `Is the file in MEDIA_ROOT after the uploading? ` @JohnGear, does the file exist? – Ivan Starostin May 03 '19 at 07:58
  • @ Ivan yes, the file gets uploaded to media/excels/ after clicking submit @marke I SSH into the VM i'm using and configured apache2.conf file that is running the whole project. How would I print media_root from the template to show it on my project page rather than in shell? – John Gear May 03 '19 at 14:31
  • 1
    `I receive exact same error after running it :(` @JohnGear this means you did not update the code / did not deploy it/ did not restart app - otherwise error message would contain full **absolute** path. – Ivan Starostin May 03 '19 at 15:04
  • You were right. I restarted Apache and everything seems to be working except it gives me same error when I click on download button to download the file. the path given to Excel – John Gear May 03 '19 at 16:29