4

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?

Danilo Bargen
  • 18,626
  • 15
  • 91
  • 127
Alexander T
  • 157
  • 2
  • 14
  • Have a look at http://stackoverflow.com/questions/10460028/confusion-in-django-admin-static-and-media-files/10460116#10460116 – Hedde van der Heide Jul 11 '12 at 17:17
  • `app-folder/static/` - good. `project-folder/static/` - bad. For future viewers, this was my mistake that lead to 404 errors. `/static/` should be [a sibling of models.py](http://stackoverflow.com/a/15435556/673991). – Bob Stein Dec 21 '15 at 19:42

1 Answers1

5

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.

Danilo Bargen
  • 18,626
  • 15
  • 91
  • 127
  • Thanks, i have edit urls.py, so I have insert: 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.STATIC_URL, document_root=settings.STATIC_ROOT), reload the page, and "Page Not Found". -> 'alessandro/Scrivania/progetto/media/photos/custodia/shop-ad.jpg' could not be found Where are the error? – Alexander T Jul 12 '12 at 16:28
  • @AlexanderT why did you replace MEDIA_URL and MEDIA_ROOT with STATIC_URL and STATIC_ROOT? – Danilo Bargen Jul 12 '12 at 21:56
  • Because the image and pdf files are a Static files? or not? – Alexander T Jul 12 '12 at 22:12
  • @AlexanderT oh, i got something mixed up. in django there's a difference between static files and media files. static files (in STATIC_ROOT) are readonly and they're generated only once when running collectstatic. media files (in MEDIA_ROOT) are usually not read only, i.e. they consist of uploads, dynamically generated files and other stuff. – Danilo Bargen Jul 13 '12 at 07:44
  • @AlexanderT in your case, you created a folder called "media" and added it to the static files dirs variable, that confused me at first. in that case, you don't even need the second urlpatterns row containing the `static()` call, as `staticfiles_urlpatterns()` should already serve all static files for you. re-check your settings.py, verify that STATIC_ROOT and MEDIA_ROOT are different folders and check the STATIC_URL configuration. – Danilo Bargen Jul 13 '12 at 07:45
  • In Settings.py: MEDIA_ROOT is empty, STATIC_ROOT is empty, STATIC_URL ='/static/', STATICFILES_DIRS = ( '/home/alessandro/Scrivania/progetto/static/photos/custodia/', '/home/alessandro/Scrivania/progetto/static/definitiva/', '/home/alessandro/Scrivania/progetto/static/proforma/', '/home/alessandro/Scrivania/progetto/static/fpdf/', # Put strings here, like "/home/html/static" or "C:/www/django/static". ) – Alexander T Jul 13 '12 at 08:46
  • Like you tell me, I rename folder media in static. Correctly. Folder media and upload static files..confuses. I'm sorry Danilo – Alexander T Jul 13 '12 at 08:48
  • But a question: I work in local, then in STATIC_URL i put http://127.0.0.1:8000, true? and in STATIC_ROOT i put ( #'/home/alessandro/Scrivania/progetto/static/photos/custodia/', .... ) ? Or i have a confuse static_url in static_root? – Alexander T Jul 13 '12 at 09:12
  • and in settings.py i have insert : from settings import STATIC_URL, STATIC_ROOT and urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) but page not foundrest here. I read a command 'python manage.py collectstatic I must do that command for collect static files at the destination location in my settings? – Alexander T Jul 13 '12 at 09:16
  • @AlexanderT `STATIC_URL` would be `/static/` and `STATIC_ROOT` is a single string to the place you would like your static files to be collected when running `./manage.py collectstatic`. Read the Django docs about staticfiles for more help. – Danilo Bargen Jul 13 '12 at 09:59