2

I can not get my site to load the static files correctly.

settings.py files in regards static files is as follows:

# Static files
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/str8red.com/static/'
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

When I run the following command:

python manage.py collectstatic

I get the following outcome:

1858 static files copied to '/var/www/str8red.com/static'

When I check the directory everything seems to have worked correctly.

I then go and checkout my site online at "https://str8red.com/" and none of the css or images are working. An example error I am seeing in chrome developer mode is as follows:

GET https://str8red.com/static/str8RED.png 404 (Not Found)

The file is being grabbed using the following code:

{% load staticfiles %}
<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <a class="navbar-brand" href="{% url 'index' %}">
        <img src="{% static 'str8RED.png' %}" width="97" height="22" alt="str8RED.com" />
      </a>

URL file:

from django.conf.urls import include, url
from django.contrib import admin
from django.views.static import serve
from dwad import settings

urlpatterns = [
    url(r'', include('meta.urls')),
    url(r'^straightred/', include('straightred.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^chaining/', include('smart_selects.urls')),
    url(r'^tinymce/', include('tinymce.urls')),
    url(r'^accounts/', include('allauth.urls')),
]

# Get Django to serve media files in debug mode.
if settings.DEBUG:
    urlpatterns += [url(r'^resources/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})]

Any help greatly appreciated, many thanks, Alan.

Alan Tingey
  • 835
  • 11
  • 39

1 Answers1

0

You need to add static url rule in your urls.py in order to tell Django to serve static files when any static url is fetched.

if not settings.DEBUG:
    urlpatterns += [
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
        url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': settings.STATIC_ROOT}),
    ]
levi
  • 22,001
  • 7
  • 73
  • 74
  • At least I am now onto a new error :) Internal Server Error: / NameError at / name 'patterns' is not defined Website show a 500 Internal Server Error, any ideas? I will go have a google, thanks for help so far. – Alan Tingey Oct 21 '16 at 21:08
  • @AlanTingey Sorri it was urlpatterns not patterns, I fixed it. – levi Oct 21 '16 at 21:10
  • To be fair that is something I should spot. Next error, not my day lol Internal Server Error: / TypeError at / 'list' object is not callable – Alan Tingey Oct 21 '16 at 21:14
  • @AlanTingey Fixed, sorry, now urls patterns are lists so I changed ( for [ – levi Oct 21 '16 at 21:15
  • Needed another ) prior to the last ]. Even after this I get the following: Internal Server Error: / NameError at / name 'urlpattern' is not defined – Alan Tingey Oct 21 '16 at 21:24
  • @AlanTingey I miss a s its urlpatterns not urlpattern – levi Oct 21 '16 at 21:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126370/discussion-between-alan-tingey-and-levi). – Alan Tingey Oct 21 '16 at 21:29
  • This is not the proper way to do it. Static files are supposed to be served by nginx or apache, not Django. – Antonis Christofides Oct 22 '16 at 09:37
  • @AntonisChristofides This is not the proper way for production but for development it is and this is what OP ask for. Why downvote? just add a comment. – levi Oct 22 '16 at 15:07
  • @levi It is clear from the title of this question that the OP is talking about production. – Antonis Christofides Oct 22 '16 at 16:48