[Python] Serve files without exposing directory structure in Django

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    Right now, for every Foo object in my database I have related file that is being stored in path that has following structure some/example/path/foo/pk/name.txt.

    The default way of serving them in Django would be by specifying MEDIA_ROOT and MEDIA_URL in settings. This way, I could access file related with Foo object with id=1 by visiting localhost:8000/media/foo/1/name.txt and similar way for other objects.

    But I would prefer not to expose to user how the files are stored internally (directory structure, primary keys etc.) and serve them from localhost:8000/media/mapped_path_to_name.txt.

    For now, the only solution I can think of is to create custom view responsible for serving files and in this view put logic that does paths mapping. Is there any more elegant(minimalistic) solution to this problem?

    Continue reading...

Compartilhe esta Página