Unable to serve content from / static root url on Django development server

I was completely puzzled for 30 minutes, but I think that my problems should be related to the changes in Django 1.3.

My urls.py looks like this:

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

If I visit / static /, I get a list of files. For example, in my root directory there is a file "iphone.png". Going to /static/iphone.png I get a 404 message.

If I changed this section to:

 if settings.DEBUG: urlpatterns += patterns('', (r'^otherstatic/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True}), (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), (r'^admin_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.ADMIN_MEDIA_ROOT}), ) 

Everything related to / otherstatic / works great.

I should also add that problems with / media / or / admin_media /.

Is this related to the Django new staticfiles application? (If so, who thought it would be a good idea to completely break this very simple use case?)

Thanks!

+4
source share
2 answers

In Django 1.3 with django.contrib.staticfiles in your INSTALLED_APPS environment will look for all static files in all applications that have a static folder.

Carefully read the first point, the second line .

Here is an example:

 project/ yourapp/ static/ iphone.png settings.py 

Assuming yourapp is located in your INSTALLED_APPS , you can only access the iphone.png image using the following URL:

 http://localhost:8000/static/iphone.png 

In your case, if you want the staticfiles application staticfiles find files under project/static/ , you need to add the following to your settings.py :

 import os SITE_ROOT = os.path.realpath(os.path.dirname(__file__)) STATICFILES_DIRS = ( os.path.join(SITE_ROOT, 'static'), ) 

Follow the above, make sure that none of the paths in STATICFILES_DIRS matches the path set in STATIC_ROOT . The latter is mainly used in production.

Do not remove the static line from the urls.py project after setting STATICFILES_DIRS .

+5
source

Unless you are using a different development server, you do not need to add anything to your urlconf to serve static files in your STATIC_ROOT directory.

http://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#s-static-file-development-view

This view is automatically enabled by runerver (with the DEBUG setting set to True). To use the view using another local development server, add the following snippet to the end of your main URL configuration:

+1
source

Source: https://habr.com/ru/post/1346365/


All Articles