Django: how to make STATIC_URL empty?

后端 未结 2 655
醉话见心
醉话见心 2021-01-24 06:56

Yep, I want it to work like in Flask framework - there I could set parameters like this:

static_folder=os.getcwd()+\"/static/\", static_url_path=\"\"

<
相关标签:
2条回答
  • 2021-01-24 07:37

    I believe this is not supported out of the box. Off the top of my head, one way to do it would be with a special 404 handler that, having failed to match against any of the defined URLs, treats the request as a request for a static resource. This would be reasonably easy to do in the development environment but significantly more difficult when nginx, Apache, and/or gunicorn get involved.

    In other words, don't do this. Nest your statics (or put them on a different sub domain) but don't mix the URL hierarchy in this way.

    0 讨论(0)
  • 2021-01-24 07:38

    This is really easy to accomplish with Nginx using try_files. Using the pseudo-settings below will make Nginx try to find a static file first, and if it fails then execution is passed to your django app.

    server {
      ...
      root /some/path/to/assets/;
      try_files $uri @django;
    
      location @django {
        ...
        proxy_pass  http://unix:/some/path/to/server.sock;
      }
    }
    

    Example: The file /some/path/to/assets/myfile.ext will be available as http://mydomain/myfile.ext

    0 讨论(0)
提交回复
热议问题