Get file path for static file in django code

I have a django application that sends an email with image files as MIME attachments. Emails are sent from views.py, but to attach files I need to get the full path name, so python can open them. These files are located in a static folder in my application, but I cannot find a way in which I can get the full path to the file in the file system, which works in development mode. It works great in the production process after collecting static data, but in dev it cannot find the file, since static files will be sent from separate application folders during development.

+13
source share
4 answers

project_dir.url add to the end of the 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 and 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 the 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 to templates

{% load staticfiles %}

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

Use module Django

from django.contrib.staticfiles import finders

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

The string resultis the path to the file system, and if it is not found, double-checksearched_locations

+14
source

.

, ( @madzohan)

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

, project_dir/static/image.py,

project_dir/settings.py STATIC_DIR varaible , . ( )

project_dir/settings.py

import os

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

app_name/views.py, /static/image.jpg

_/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')

image.jpg

, .

0

Since you want a static application, here is 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)}')

FILE_NAMEdoes not have to exist. Or you can use stores[APP_LABEL].path('')to get the path.

0
source

All Articles