Re: Static files not loading when debug is false

1,834 views
Skip to first unread message

Jani Tiainen

unread,
Sep 9, 2012, 8:30:37 PM9/9/12
to django...@googlegroups.com
I suppose that your frontend webserver is serving files from url /static/ from path that STATIC_ROOT points to?

On Sun, Sep 9, 2012 at 10:23 PM, Karambir Singh Nain <akar...@gmail.com> wrote:
I have a fairly simple django project having some views, templates and static files like  css and images. My settings file include : 

STATIC_ROOT = '/home/karambir/Codes/projects/cdi/cdi/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    '/home/karambir/Codes/projects/cdi/cdi/data',
)
TEMPLATE_DIRS = (
    '/home/karambir/Codes/projects/cdi/cdi/templates'
)

So I serve static files with  {{ STATIC_URL }} in the templates. And it is working fine when DEBUG is TRUE but every static file breaks when debug is set to false. Then I tried with django admin, it was also broken. So I run a ./manage.py collectstatic command. And then admin css works fine but my own files still not. I saw in the url of the loaded html page and it shows correct url and it is not loading. 
How can I know what is the main problem. What changes takes place when debug is set to false?
(I'm running django1.4)

Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/fWCsL9PUI1EJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

Jani Tiainen

unread,
Sep 9, 2012, 8:35:44 PM9/9/12
to django...@googlegroups.com
Also just a note, if you're using manage.py runserver without debug = TRUE setting, static file serving is not taking place. you can use --insecure option to mark that you really want to do staticfile serving still from django.

Stephen Anto

unread,
Sep 10, 2012, 4:08:02 AM9/10/12
to django...@googlegroups.com
Hi Singh,

I have configured static file for my projects as follows.
In settings.py

import os
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_URL = '/site_media/'

STATIC_ROOT = ''

# URL prefix for static files.
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

and urls.py

urlpatterns = patterns('',
    # For static file serving in development. Deactivate in production environment
    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT }),
        
    )

I believe it may help you..

On Mon, Sep 10, 2012 at 12:53 AM, Karambir Singh Nain <akar...@gmail.com> wrote:
I have a fairly simple django project having some views, templates and static files like  css and images. My settings file include : 

STATIC_ROOT = '/home/karambir/Codes/projects/cdi/cdi/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    '/home/karambir/Codes/projects/cdi/cdi/data',
)
TEMPLATE_DIRS = (
    '/home/karambir/Codes/projects/cdi/cdi/templates'
)

So I serve static files with  {{ STATIC_URL }} in the templates. And it is working fine when DEBUG is TRUE but every static file breaks when debug is set to false. Then I tried with django admin, it was also broken. So I run a ./manage.py collectstatic command. And then admin css works fine but my own files still not. I saw in the url of the loaded html page and it shows correct url and it is not loading. 
How can I know what is the main problem. What changes takes place when debug is set to false?
(I'm running django1.4)

Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/fWCsL9PUI1EJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Thanks & Regards
Stephen S



Blog:      blog.f2finterview.com

e.generalov

unread,
Sep 10, 2012, 7:20:01 AM9/10/12
to django...@googlegroups.com
There is another case - tests. The TestCase forces DEBUG=False, therefore staticfiles application, included to the project in accordance with the documentation, works in the development server, but unexpectedly does't serve static in test scenarios. Why the serving static files in the development environment is associated with the debugging?

понедельник, 10 сентября 2012 г., 10:08:27 UTC+6 пользователь Stephen Anto написал:

Karambir Singh Nain

unread,
Sep 10, 2012, 12:57:37 PM9/10/12
to django...@googlegroups.com
Yeah. during, debug=true, it is serving fine from static_root. But not when debug is false.

Jani Tiainen

unread,
Sep 10, 2012, 8:28:21 PM9/10/12
to django...@googlegroups.com
As I put in my second post. Static serving is not working (by default) when you turn debugging off unless you provide --insecure option on manage.py runserver command. That is by design. Though with you should see improperly configured exception raised if you try to use forcefully static serving view (url is not even added when debug is false thus you should just see 404 errors if you use recommended staticfiles configuration pattern).

Read carefully implications of using staticfiles app and how it replaces runserver command:


HTH.

To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/W-IOauTb9dQJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply all
Reply to author
Forward
0 new messages