1

I am trying to get my project up in production on my localhost, however the static files are all returning 404 except the CDNs.

--settings.py--

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1']

STATIC_ROOT = os.path.join(BASE_DIR, "static","static_root")

STATIC_URL = "/static/"

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static", "our_static"),
    # '/var/www/static/',
)

Then I have some extra code in my urls.py, maybe it could be causing trouble

--urls.py--

]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Brian Pace
  • 39
  • 1
  • 9
  • 1
    `STATIC_URL` should be something like `'/static/'`. It shouldn't rely on `STATIC_ROOT` (which is the location on the server where the static files are served from). – Alasdair Sep 07 '15 at 10:24
  • oh I forgot to change it back to static, I tried to test it out to see if it made any difference, but both bringing up 404 errors – Brian Pace Sep 07 '15 at 10:27
  • 1
    You haven't shown your server config (e.g. Apache or Nginx) which you are using to serve static files in production. – Alasdair Sep 07 '15 at 10:33
  • I am only running the server on django for now, to get things up and running. – Brian Pace Sep 07 '15 at 10:34
  • 1
    You've misunderstood how static files work. In development, Django's runserver can serve static files. In production, your server (e.g. Apache or Nginx) is responsible for serving static files. You can't get things 'up and running' in production with Django alone. – Alasdair Sep 07 '15 at 10:36
  • 3
    The Django server is *not* for production use. Do not run it on your production machine. – Daniel Roseman Sep 07 '15 at 10:40
  • Thanks for the help. I will set up my Apache then. – Brian Pace Sep 07 '15 at 10:40
  • As an aside, you can remove the first `+= static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)`, since you are doing the same thing below in your `if settings.DEBUG` block. – Alasdair Sep 07 '15 at 10:44

2 Answers2

0

If you are running Django with Apache and WSGI, you can serve static files following Django debug page layout is broken, and put all your static files under /path/to/mysite.com/static/

Community
  • 1
  • 1
Yuwen Yan
  • 4,777
  • 10
  • 33
  • 63
0

Your settings.DEBUG is set to False. Thus static and media urls are not mapped to respective root settings. Either make DEBUG = true in setting files or remove if condition in urls.py.

pacholik
  • 8,607
  • 9
  • 43
  • 55