Alexander T
Alexander T

Reputation: 167

Static Files - Page Not Found

To see the static files (images and pdf), I defined STATIC_DIRS with the values containing directories' names where I upload those files:

STATICFILES_DIRS = (
    '/home/alessandro/Scrivania/progetto/media/photos/custodia/',
    '/home/alessandro/Scrivania/progetto/media/definitiva/',
    '/home/alessandro/Scrivania/progetto/media/proforma/',
    '/home/alessandro/Scrivania/progetto/media/fpdf/';
)

In STATIC_URL:

STATIC_URL = '/static/' 

In Installed Apps:

INSTALLED_APPS = (
   ....

    'django.contrib.staticfiles',

)

The permissions are 0777.

Now When I want to see the image or pdf files, I get this error message. Page not found

I am using this URL:
http://127.0.1:8000/home/alessandro/Scrivania/progetto/media/photos/custodia/powered_by.png

Any Ideas? Why is this problem occuring?

Upvotes: 4

Views: 3028

Answers (1)

Danilo Bargen
Danilo Bargen

Reputation: 19482

By default, static and media files are not served with Django's builtin dev server. If you want it to serve those files directly, add staticfiles_urlpatterns and a MEDIA_URL pattern to your urlconf.

from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static

# ... the rest of your URLconf goes here ...

urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This will only work if DEBUG is True.

For more information, refer to the corresponding docs.

Note: You should not do this in production! Always use a separate webserver for static files in production mode.

Upvotes: 5

Related Questions