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
  • 3,084
  • 1
  • 32
  • 65
  • 2
    Does this answer your question? [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) – gasman Jan 24 '20 at 08:21
  • 1
    I would definitely check your browser's web inspector. – Rounin Jan 24 '20 at 09:25
  • check the network tab in the browser webtool and look for the failing css request. – aviya.developer Jan 24 '20 at 09:44
  • @gasman Yes indirectly. Thank you, I have posted my own answer – Psionman Jan 24 '20 at 09:49

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
  • 3,084
  • 1
  • 32
  • 65
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,910
  • 1
  • 16
  • 36