python - Django: Restrict static folder access to non logged-in users -
i trying restrict users, directly hit on absolute static image url path(www.xyz.com/static/img/sam.png) in browser , access it.
i tried following django docs:
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/apache-auth/
but block images there in login page also(before valid user authenticated).
is there other efficient way restrict non logged-in users?
edit: had referred django: serving media behind custom url related nginx , not apache. , there difference b/w static , media content. question related static content
you can try answer here routing static
url request own view (it tries use sendfile extension available in web servers) or use django whitenoise, whitenoise uses sendfile api server independent ( whether using nginx or apache) , production ready, extend whitenoise middleware
, add checking there file restriction, sample
code be
django.http import httpresponseforbidden whitenoise.middleware import whitenoisemiddleware # sample code, can change use case class protectedstaticfilemiddleware(whitenoisemiddleware): def process_request(self, request): # check user authentication if condition_met(request): return super(whitenoisemiddleware, self).process_request(request) # condition false return httpresponseforbidden("you not authorized")
note: serving files directly ( large files ) using python file chunks api not idea when in production ( ideas file.read() or fileresponse)
Comments
Post a Comment