yak
yak

Reputation: 3940

Django STATIC_URL does not work, does not serve files

I have a company wordpress blog at http://example.com and Django application successfully deployed at http://example.com/web.

My application is a simple app (I still learn Django) where I would like to have a background for the 'welcome' pages (and other pages too).

I read in docs that I need to use STATIC_URL - so here's what I have and what I did:

  1. My project structure:

enter image description here

  1. Entires in settings.py file which are relevant for serving static files:
STATIC_URL = '/web/static/'
SETTINGS_PATH = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_PATH, os.pardir)
STATICFILES_DIRS = (os.path.join(PROJECT_PATH, "static"), )
USE_X_FORWARDED_HOST = True
FORCE_SCRIPT_NAME = '/web'
SUB_SITE = "/web"

And here's the template file, base.html in which I would like to use back.jpg as webpage background image:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Simple Site
        {% block title %}{% endblock %}</title>
    </head>
    <body background="/static/back.jpg" text="white"/>

                <div align="center">

        <h1>{% block head %}{% endblock %}</h1>
        {% block content %}{% endblock %}

                </div>

    </body>
</html>

However, I can't see the image. It's a production (not dev) app. How can I solve this?

Upvotes: 1

Views: 1105

Answers (2)

Sayse
Sayse

Reputation: 43320

Your background is looking in the wrong place for the background image, you should use the static template tag

<body background="{% static 'back.jpg' %}" text="white"/>    

To use this, you need to load in static (preferably at the top of your template)

{% load static %}

Upvotes: 3

Ewan
Ewan

Reputation: 15058

Using whatever webserver (Apache or Nginx) you need to configure them to server your static files.

Nginx

Look at the serving static content section of the nginx resources.

location /web/static {
    root /path/to/example.com/static/;
}

Apache

Django has good documentation on deploying with Apache and mod_wsgi including how to serve static files.

Alias /web/static/ /path/to/example.com/static/

<Directory /path/to/mysite.com/static>
Require all granted
</Directory>

Upvotes: 2

Related Questions