Hosting Django from a directory

  Kiến thức lập trình

My directory structure is like so:

C:htdocs                                     # web root
   |_ django-container
      +- django-project
      |  +- core                              # startproject
      |  |_ myapp1                            # startapp
      +- public
         +- static                            # STATIC_ROOT, collectstatic
         |  +- css
         |  +- images
         |  |_ js
         |_ media                             # MEDIA_ROOT

Nginx config:

http {
    # ...
    upstream wsgi-server {
        server 127.0.0.1:8080;
    }
    server {
    # ...
        location /myloc/ {
            proxy_pass http://wsgi-server/;
        }
        location /myloc/public/media/ {
            alias C:/htdocs/django-container/public/media/;
            autoindex off;
        }
        location /myloc/public/static/ {
            alias C:/htdocs/django-container/public/static/;
            autoindex off;
        }
    }
}

I run a waitress WSGI server as a service on port 8000 that runs core.wsgi.application. My settings.MEDIA_URL and settings.STATIC_URL are 'public/media/' and 'public/static/' respectively.

When I do redirects in my app, or create urls in templates, it doesn’t have the myloc segment, because of which all links fail. Even the /static/ and /media/ links. For the last 2, I could add the segment to settings.STATIC_URL and settings.MEDIA_URL, but I wanted a common way to add the myloc segment at all places, preferably automatically if possible.

LEAVE A COMMENT