Reputation: 185
I'm working on an application where we want the following to happen:
http://<host>/site/<user-friendly-url>/
, with the index.html at the root, all the paths to css and js files, etc.I know how to configure the route, but I was wondering what to put in the view. I have tried with STATIC_{ROOT,URL} and MEDIA_{ROOT,URL} but I feel it's not the right way.
In summary : is there a way to put something like return serve_this_static_directory()
in the view? or is there any workaround there?
Thanks for your answers
edit:
Trying to clarify my question.
http://<host>/site/<user-friendly-url>/
) doesn't match the directory name (which could be anything, like XYZ_html/
).For those two reasons, I don't have any idea how to serve this content using the webserver, because how is it possible to configure the routes then?
Upvotes: 0
Views: 291
Reputation: 20569
First you must realize that django won't serve your static or media files by default, you should use your HTTP server (nginx, apache or other) for that.
Next thing to do is to configure your HTTP server to serve "/path/to/your/project/public" and any files inside on root URL of your domain, and instead of serving 404 error if file is not found, it should redirect to django (so if there is some file in path /site/something/ your HTTP server should serve that file, if not, it shoudl serve anything that django will output on that path. Sample configuration for nginx might look like this:
server {
listen 80;
server_name oskar.local;
root /path/to/your/project/public;
location @default {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
include /etc/nginx/uwsgi_params;
uwsgi_pass app_server; # or some proxy_pass if you're not using uwsgi
break;
}
location / {
try_files $uri @default;
}
}
And last step is to configure your django app's MEDIA_* settings:
MEDIA_ROOT = "/path/to/your/project/public"
MEDIA_URL = "/"
But be careful! With that approach there is potential vulnerability: any user can put files into path that is normally handled by django. Better approach will be to put all of "sub-sites" inside some sub-directory or into separate domain.
Upvotes: 1