Django: Checking if static file exists

Viewed 417

Hope you're doing well. I've got a django view that needs to check to see if a static file with a known path exists.

I'm currently trying to use os.path.isfile(), but I have a feeling this is the wrong approach.

views.py:

import os
def knighthood_ceremony(warrior):
    narration_path = static("polls/narrations/{}/{}.mp3".format(queen_name,warrior.name))
    is_to_play_narration = os.path.isfile(narration_path)

settings.py:

STATIC_URL = '/static/'

Structure:

mysite
    mysite
        settings.py
    polls
        static
            polls
                narrations
                    5
                        1.mp3
        views.py
    otherproject

This gives local vars:

narration_path = '/static/polls/narrations/5/1.mp3'
is_to_play_narration = False

But, there is a file at:

C:\Users\{USER}\mysite\polls\static\polls\narrations\5\1.mp3

Why can't os.path.isfile see it? Any advice will be greatly appreciated!

1 Answers

Are you trying to read it via the URL or the STATIC_ROOT? If you are reading from the harddrive. Might be the error there

Related