I have a Django 4.1 project and want to make an alias to the directory with static files. So, in settings.py
I have
STATIC_URL = "static/"
All my static files are in the example.com/static/...
. I want to make a shortcut to one of directories of static files. For example example.com/magic/...
should be the same as example.com/static/physics/...
. That shoudn't be a redirect, because if it is a redirect, I won't be able to download file using curl
without any special options.
How can I do this? Maybe with some special paths in urls.py
?
You can put the static url to
Making shortcuts do directories/redirecting without a redirect like that is better done with a server like Nginx. Example config for nginx:
This will basically pass-through the requests in nginx, straight to the domain (example.com), this way, the redirect happens in the backend, and the user (or curl in your case) won't notice a thing. It doesn't involve the slow python interpreter, nginx is made straight in C.
If you go the nginx route,
https://django-server-example.com/static/
is the static url, nothttps://example.com/static/
, it will be run on the same domain, but it will (again), pass through the requests. Sohttps://django-server-example.com/static/
will lead tohttps://example.com/static/
(without a redirect).If you do nginx, set the static url to
STATIC_URL = "/static/"
So remember, django does not handle your staticfiles anymore. But still do set the
STATIC_URL
, this way your{% static %}
templatetag works.