18

I'm creating a Django project. I just tried taking the project out of debug, DEBUG = False and for some reason all my static files do not show up. They give an error code of 500. How do i fix this?

some of settings.py:

DEBUG = True
TEMPLATE_DEBUG = DEBUG
...
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
#    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)
Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98
  • can you be more specific, what part? it's pretty long. – Nachshon Schwartz Jun 19 '11 at 21:38
  • 1
    are you serving static files via the django development server or your own webserver. If it's the dev server, where in your URLs are you taking care of the static files? You might find that once you turn off DEBUG, the URLs dealing with serving the static files are disabled – Timmy O'Mahony Jun 20 '11 at 00:05
  • I am not really sure where I take care of staticfiles, I am using the Django development server. I think that whats happening is what you wrote, the URL's ddealing with staticfiles are disabled upon turning DEBUG off, any idea what i should do? – Nachshon Schwartz Jun 20 '11 at 06:54
  • Here's an answer from the duplicate question: https://stackoverflow.com/a/5836728/1224827 – Blairg23 Dec 14 '17 at 09:29
  • Possible duplicate of [Why does DEBUG=False setting make my django Static Files Access fail?](https://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail) – Blairg23 Dec 14 '17 at 09:30
  • @Blairg23 how can a question from 2011 be a duplicate of question from 2017. Should be the other way around... – Nachshon Schwartz Dec 14 '17 at 10:20
  • @Nayish , it says it was asked on April 2011 and yours was June 2011 – Blairg23 Dec 14 '17 at 15:23

2 Answers2

38

Static files app is not serving static media automatically in DEBUG=False mode. From django.contrib.staticfiles.urls:

# Only append if urlpatterns are empty
if settings.DEBUG and not urlpatterns:
    urlpatterns += staticfiles_urlpatterns()

You can serve it by appending to your urlpatterns manually or use a server to serve static files (like it is supposed to when running Django projects in non-DEBUG mode).

Though one thing I am wondering is why you get a 500 status code response instead of 404. What is the exception in this case?

EDIT

So if you still want to serve static files via the staticfiles app add the following to your root url conf (urls.py):

if settings.DEBUG is False:   #if DEBUG is True it will be served automatically
    urlpatterns += patterns('',
            url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )

Some things you need to keep in mind though:

  • don't use this on a production environment (its slower since static files rendering goes through Django instead served by your web server directly)
  • most likely you have to use management commands to collect static files into your STATIC_ROOT (manage.py collectstatic). See the staticfiles app docs for more information. This is simply necessary since you run on non-Debug mode.
  • don't forget from django.conf import settings in your urls.py :)
Torsten Engelbrecht
  • 13,318
  • 4
  • 46
  • 48
  • I'm not sure I understand, is this an answer or a question, where is the part where you say: this is what you should do...? – Nachshon Schwartz Jun 20 '11 at 06:51
  • 1
    "You can serve it by appending to your urlpatterns manually or use a server to serve static files (like it is supposed to when running Django projects in non-DEBUG mode)." was the answer? I am adding an example for you if needed, though its the same as serving static files before Django 1.3. And yes, the last one is a questions. So what exception is thrown when it comes to this 500 response? Just interested. – Torsten Engelbrecht Jun 20 '11 at 08:08
  • Great, that did the trick, I just added that piece of code to my urls.py file and it works like a charm... thanks. – Nachshon Schwartz Jun 20 '11 at 09:16
  • My guess about the 500 is that—since DEBUG is True—django is then trying to render a 404.html template, which raises TemplateDoesNotExist error, so it tries to then render a 500.html template, which also doesn't exist, and just raises a 500. You’d think django would deal with this situation better, but it doesn’t. – MrColes Jan 18 '13 at 20:40
  • Not yet anyway. The coming Django version (1.5) is supposed to handle it better. – Torsten Engelbrecht Jan 19 '13 at 10:11
  • I was getting 404 but after adding the url pattern, works fine in apache for Django 1.6.5.... – Pankaj Anand Feb 09 '15 at 10:13
  • 1
    @PankajAnand Its still advised to serve static files via apache directly, rather than through Django. At least once you have production traffic running against your instance it will benefit the performance. – Torsten Engelbrecht Feb 09 '15 at 10:16
  • @TorstenEngelbrecht Yes I agree, actually apache level sounds better for performance.. – Pankaj Anand Feb 09 '15 at 10:21
  • Just to add in this answer from a duplicate question: https://stackoverflow.com/a/5836728/1224827 – Blairg23 Dec 14 '17 at 09:29
12

In Django 1.3, if you are just testing using the manage.py runserver, you can add the option "--insecure", as described in the staticfiles docs:

It seems to still send emails to the admin saying that there is no template, but it does serve static files.

I'm not sure if the email issue is on purpose or a bug

duduklein
  • 10,014
  • 11
  • 44
  • 55