1

I have implemented wagtail on my site and when I put it into production it is not finding the css or any images. I have run

python manage.py collectstatic

and all of my css is in a directory called static_files in the same directory as manage.py.

In base.py I have:

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
....
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "static_media")

On my development site BASE_DIR is the directory with static_files, but it does not seem to pick it up on the production site.

Is there any way that I can test to see where the production version is looking for the css, e.g in the HTML?

[Edit] I have looked at the answers to this question and it does not address the core of my problem which is that certain essential code was not being called when DEBUG is set to False

Psionman
  • 2,342
  • 1
  • 24
  • 46

2 Answers2

0

You need to include the following code in urls.py in your project directory:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
...
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

For some reason this was in an if section under:

if setting.DEBUG = True

and I had DEBUG set to False. If I move it to the main body of the module, it works perfectly

Psionman
  • 2,342
  • 1
  • 24
  • 46
0

In the references to the static assets in templates, be sure to use the {% static ... %} template tag (reference). In the references to media files in templates, be sure to prefix with {% get_media_prefix %} (reference). Also, I typically use the same final designator for both STATIC_URL and STATIC_ROOT (i.e. /static/, not /static/ for one and static_files for the other). Here is how I declare these variables:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, STATIC_URL.replace('/', ''))

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, MEDIA_URL.replace('/', ''))
Dan Swain
  • 2,592
  • 1
  • 13
  • 34