Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
Django: Restrict static folder access to non logged-in users

I am trying to restrict those users, who directly hit on a absolute static image url path(www.xyz.com/static/img/sam.png) in browser and access it.

I tried with the following django docs:

https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/apache-auth/

But this will block those images which are there in login page also(before valid user is authenticated).

Is there any other efficient way to do restrict non logged-in users?

EDIT: I had referred to this Django: Serving Media Behind Custom URL but it is related to nginx and not apache. And also there is a difference b/w static and media content. My question is related to just static content

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

you can try my answer here by just routing static url request to your own view (it tries to use sendfile extension available in almost all web servers) or use django whitenoise, whitenoise uses sendfile api which is server independent ( whether you are using nginx or apache) and production ready, extend the whitenoise middleware and add your checking there for file restriction, a sample code will be

  from django.http import HttpResponseForbidden
  from whitenoise.middleware import WhiteNoiseMiddleware
  # this is a sample code, you can change for your 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 are not authorized")

NOTE: Serving files directly ( large files ) using python file chunks api is not a good idea when you are in production ( ideas like file.read() or FileResponse)

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda