I am trying to build my first Django project from scratch and am having difficulty setting the background image. I am new to programming in general so forgive me if this is a stupid question.
I have read the documentation here on static file implementations and various stack overflow posts (here here and here) on setting the background image, but I still can’t get it to work.
I have:
-
installed the django.contrib.staticfiles as an installed app.
-
added this to the settings file:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
) -
set the Static_Root and Static_URL files to STATIC_ROOT = ‘/Users/user_name/development/projects/ecollar_site/static/’ and ‘/static/’ respectively
-
put this code at the end of my URLs file:
`if settings.DEBUG: urlpatterns += patterns('', (r'^/static/(?P<path>.*)$', 'django.views.static.serve', {'/ecollar_site/': settings.STATIC_ROOT, 'show_indexes' : True}), )` -
ran
python manage.py collectstatic -
and then put this line in the CSS file.
body{ font:16px/26px Helvetica, Helvetica Neue, Arial; background-image: url("{{ STATIC_URL }}img/IMG_0002.jpg"); }
I know the static files bit is working because the CSS is loaded. And I can change the background color by using background-color: blue. But the static image is simply not being put as the background image. Have I made a rookie mistake somewhere?
Your css file is not rendered by Django template engine and so {{ STATIC_URL }} is not being replaced. You’ll have to use /static/img/IMG_0002.jpg in the CSS file or move that bit of CSS in your html file’s style tag.