0

I have troubles searching for static files in multiple folders in development mode.

Of course I'm aware of the trivial approach to use os.path.listdir or other methods to perform a search, but I am looking for a way to keep development and production code in line as much as possible.

I have the following scenario: Multiple apps with separate static folders and a project wide static folder, which works just fine.

But I have a method that looks for specific files and I want to make sure all folders are searched. Of course I could construct those paths by reading settings.STATICFILES_DIR[] and figure out with apps have 'static/' folders, but I wonder if that would mean reinventing the wheel.

Is there a special method for dev mode that delivers a list of all static folders available or something like that?

If possible I would like to avoid a special debug case in my code because it doesn't appear very pythonic to me. But please correct me if I'm wrong here.

Sudipta
  • 4,773
  • 2
  • 27
  • 42
Dr.Elch
  • 2,105
  • 2
  • 17
  • 23

1 Answers1

0

Why do you search static files in development mode?

It's ease to use:

python manage.py colelctstatic -l

Just like in production.

Django development server is very slow for serving static. And you add static files not so often. It will be little difficult to not forget type this command when static files not loaded. But with time you will remember it and will be happy. Believe me ;)

I use nginx configuration like this:

server {
    server_name my-site.de;

    location /{
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8021/;  #<- unique port for project    
    }

    location /static {
        autoindex off;
        alias /home/noname/develop/my-site/my-site/proj_static;
        if ($query_string) {
            expires max;
        }
    }

    location ^~ /media/ {
        autoindex off;
        alias /home/noname/develop/my-site/my-site/media/;
        if ($query_string) {
            expires max;
        }
    }
}

With record in /etc/hosts: 127.0.0.1 my-site.de

For every site i develop. Therefore i can ease open local version just by changing 'com' to 'de' in url.

And if you add some lines to manage.py:

if sys.argv[1] == 'runserver' and len(sys.argv)==2:
    sys.argv.append('0.0.0.0:8021')

You will can run multiple projects at the same time, And access them by url just like: http://project1.de, http://project2.de

Yevgeniy Shchemelev
  • 3,601
  • 2
  • 32
  • 39