skip to Main Content

I have deployed a django project with mod wsgi on an apache server. Everything works so far.
In one python script I’d like to open a file in static files.

Therefore I have tried something like this:

mycode.py

import cv2


    class Foo():
      def __init__(self):
        ...

      def openImg(self):
        self.myimg = cv2.imread('static/myapp/img/picture.jpg',0)
        return self.myimg

.. just for testing

However it can’t find this file. 

Whether in path (‘static/myapp/img/picture.jpg’), nor path (‘myapp/static/myapp/img/picture.jpg’) nor in path (‘../static/myapp/img/picture.jpg’) and some other which I have already forgot.

It always returns "None".

If I try the same on my "undeployed" local project it works without any problem. 

I have already collected all staticfiles to /myproject/static and given enough rights to access them. 

By the way, the access to the static files in the html works in production.

What’s the problem?
Can you give me any advice?

Thank you.


Some additional information:

views.py

from django.shortcuts import render
from .mycode import Foo

def index(request):

   # New Instance for class Foo
   bar = Foo()

   # Return img array to put it in html for whatever 
   imagearray = bar.openImg()

   context = {'imgarray': imgarray}
   return render(request, 'myapp/index.html', context)

Thats an example of my directory

/home/myuser/MyProject
  /static/myapp/img/picture.jpg (collected)
  /
  /myapp
    /static/myapp/img/picture.jpg
    /mycode.py
    /views.py
  /MyProject
    /settings.py

  

In my apache config file:

Alias /static /home/myuser/MyProject/static
<Directory /home/myuser/MyProject/static>
    Require all granted
</Directory>

Alias /media /home/myuser/MyProject/media 
<Directory /home/myuser/MyProject/media>
    Require all granted
</Directory>

<Directory /home/myuser/MyProject/MyProject>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>


WSGIScriptAlias / /home/myuser/MyProject/MyProject/wsgi.py
WSGIDaemonProcess django_app python-path=/home/myuser/MyProject python-home=/home/myuser/MyProject/venv
WSGIProcessGroup django_app

2

Answers


  1. This was happening to me until i ran

    python manage.py collectstatic
    

    could you try this?

    Login or Signup to reply.
  2. In mycode.py you might want to try:

    from django.contrib.staticfiles.storage import staticfiles_storage
    import cv2
    
    
        class Foo():
          def __init__(self):
            ...
    
          def openImg(self):
            path = staticfiles_storage.path('myapp/img/picture.jpg')
            self.myimg = cv2.imread(path,0)
            return self.myimg
     
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search