0

In my setting.py I have next code:

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATICFILES_DIRS = [
    os.path.join(PROJECT_DIR, 'static'),
]

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

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

And in views.py I want to get some of the images inside static folder as follows:

for f in ["static/img/logo/contact-form/logo_black.png", "static/img/logo/contact-form/logo_white.png":
    fp = open(os.path.join(BASE_DIR, f), 'rb')
    msg_img = MIMEImage(fp.read())
    fp.close()
    msg_img.add_header('Content-ID', '<{}>'.format(f))
    msg.attach(msg_img)

But I'm getting an error:

"[Errno 2] No such file or directory: '/Users/Admin/Projects/web-dealers/webDealers/static/img/logo/contact-form/logo-black.png'"

UPDATE

The urls.py is as follows:

urlpatterns = [
    url(r'^django-admin/', admin.site.urls),

    url(r'^admin/', include(wagtailadmin_urls)),
    url(r'^documents/', include(wagtaildocs_urls)),

    url(r'^search/$', search_views.search, name='search'),
    url(r'^api/', include('API.urls')),
    url(r'^contact-us/', contact_us, name="contact_us"),

    # Languages
    url(r'^i18n/', include('django.conf.urls.i18n'), name='set_language'),
    url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),

    # For anything not caught by a more specific rule above, hand over to
    # Wagtail's page serving mechanism. This should be the last pattern in
    # the list:
    url(r'', include(wagtail_urls)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

What am I doing wrong?

Boky
  • 11,554
  • 28
  • 93
  • 163
  • you need to define them in urls to access them, or use external service like NGINX or Apache to serve them – ruddra Nov 13 '18 at 10:36
  • @ruddra Check the updated question. They are defined in urls. – Boky Nov 13 '18 at 10:43
  • What is "the rest of the code"? In particular, how are you opening those files? – Daniel Roseman Nov 13 '18 at 10:47
  • @DanielRoseman I've updated my question – Boky Nov 13 '18 at 10:49
  • Why do you include the "static" prefix and join with BASE_DIR? Why not `"/img/logo/contact-form/logo_black.png"` then join with STATIC_ROOT? Also note, STATIC_DIR should not be the same as STATICFILES_DIRS. – Daniel Roseman Nov 13 '18 at 10:58
  • Might it be a problem with referring to static files in views. Check out [this question](https://stackoverflow.com/a/17738606/216846) for discussion how to use static files in views. – Juho Rutila Nov 13 '18 at 10:44

1 Answers1

0

Static in django are always tricky and a hell. I do not know why:) Don't you have DEBUG=False and forgot to do python manage.py collectstatic?

However, this works for me:

settings.py:

INSTALLED_APPS = [
    ...some other applications....

    'django.contrib.staticfiles',

    ...some other applications....

]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

I do not have anything in urls.py. I can access static files normally like: e.g. localhost:8000/static/image.png.

EDIT:

It seems that I did not read that properly and answered something different:) Your problem is not during the static files serving but when you access internally at the backend. It says: file not found. Are you sure that the path is correct? Or, that the os.path.join(BASE_DIR', 'static') really contains the file? You try to access the file using and absolut path so it must be easily verifiable is it is there or not at:

'/Users/Admin/Projects/web-dealers/webDealers/static/img/logo/contact-form/logo-black.png'
Radek Hofman
  • 517
  • 3
  • 12