Forums

Middleware not working because request doesn't reach it (static files problem)

I have a deployed project using django 3.2.8 with python anywhere and i want to restrict users from reaching static files on server (ex: www.python.com/images/test.png), so that any one with the link, he can't reach the image and i used middleware and it works perfect on development, which means if you have the image link, it will redirect you to the "404 page" .. but when it comes to production, it doesn't work as needed! i was told that my static files aren't served by django on production so the request doesn't even reach the middleware to redirect the user the the 404 page so... how can i make the request reach the middeware?

I would suggest separating the static files that you want to serve from your web app (and not through static files) into their own directory. Then you can avoid using the PythonAnywhere static files system for that directory and write a view in your django code to serve those files. Then they will be processed through the middleware.