2

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!

Zephyr
  • 11,891
  • 53
  • 45
  • 80
JimmyCarlos
  • 1,934
  • 1
  • 10
  • 24

1 Answers1

-1

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

  • Could you explain this some more please? I don't understand what that means. – JimmyCarlos Jul 11 '20 at 15:06
  • Sorry if unclear. Basically your STATIC_URL refers to the directory in the url path. i.e. www.mysite.com/STATIC_URL but if you are trying to get a path on the actual harddrive then there might be a difference there. From the docs. https://docs.djangoproject.com/en/dev/ref/settings/#static-root Also this question might help you https://stackoverflow.com/questions/25470089/static-root-and-static-url-confusion-in-django – Simon Tuohy Jul 11 '20 at 16:41