[Django]-Get the file path for a static file in django code

45👍

Use finders-module of Django

from django.contrib.staticfiles import finders

result = finders.find('css/base.css')
searched_locations = finders.searched_locations

String result is the file-system path, and if not found, double check searched_locations

23👍

project_dir.url add to the end of file

if DEBUG:
    urlpatterns += patterns(
        '',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': MEDIA_ROOT}),

        url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': STATIC_ROOT}),
)

project_dir.settings

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static_debug')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

make dirs media & static_debug (add them to .gitignore)

project_dir/
    static/
        image.jpg
    settings.py
    urls.py
apps/
    __init__.py
    some_app/
        static/
            some_app/
                css/
                    style.css
media/
static_debug/

Now you can run project or directly

python manage.py collectstatic

access from views

from django.templatetags.static import static

static('image.jpg')
static('some_app/css/style.css')

access from templates

{% load staticfiles %}

{% static 'image.jpg' %}
{% static 'some_app/css/style.css' %}

7👍

After doing lots of mistakes the following thing worked for me..

Suppose you have following directory structure (Thanks @madzohan)

project_dir/
    static/
        image.jpg
    settings.py
    urls.py
apps/
    __init__.py
    some_app/
        static/
            some_app/
                css/
                    style.css

And now if you want to get the path of project_dir/static/image.py then

In your project_dir/settings.py file define a STATIC_DIR varaible on the file scope like. ( If not done before )

project_dir/settings.py

import os

# after your other file variables
STATIC_DIR = os.path.join(BASE_DIR, 'static')

Now in your app_name/views.py file from where you want to acess the /static/image.jpg file

app_name/views.py

import os
from project_dir.settings import STATIC_DIR

# here I want the /static/image.jpg file then
image = os.path.join(STATIC_DIR, 'image.jpg')

And then you can have acces to image.jpg file throught image variable

Hope this works well for you

2👍

Since you want an app’s static, here’s what you need!

from django.contrib.staticfiles import finders

APP_LABEL = 'app_youre_looking_at'
FILE_NAME = 'file_name_you_want_to_read_or_write.ext'

stores = finders.AppDirectoriesFinder(app_names={APP_LABEL}).storages
print(f'Here it is: {stores[APP_LABEL].path(FILE_NAME)}')

The FILE_NAME is not required to exist. You can use stores[APP_LABEL].path('') to get path only.

Leave a comment