1

I'm new to Django. I have some difficulties, my static files are not loaded properly.

My project directory looks like this:

  • myNewWebSite
    • myNewWebSite
    • home
    • static
      • css
        • home.css
    • templates
      • home
        • index
  • manage.py
  • init.py

I configured following settings in myNewWebSite/settings.py:

PROJECT_APP_PATH = os.path.dirname(os.path.abspath(__file__))
PROJECT_APP = os.path.basename(PROJECT_APP_PATH)
PROJECT_ROOT = BASE_DIR = os.path.dirname(PROJECT_APP_PATH)
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, STATIC_URL.strip("/"))

My template (templates/home/index.html):

{% load staticfiles %}
<html>
<head>
    <link rel="stylesheet" type="text/css" href="{% static 'css/home.css' %}" />
</head>
<body>
    <h1 id="test">Hello World, This is my first website in django</h1>
</body>
</html>

My styles in static/css/home.css:

body {
    background-color: red;
}

#test {
    border: 10px solid green;
}

I cannot see any effect of these changes. Only the message Hello World,... shows up. The output shows this:

[08/Jun/2016 20:36:04] "GET /myhome/ HTTP/1.1" 200 178
[08/Jun/2016 20:36:04] "GET /static/css/home.css HTTP/1.1" 301 0
[08/Jun/2016 20:36:04] "GET /static/css/home.css/ HTTP/1.1" 404 4930

I have set DEBUG = False in settings.py and local_settings.py(in Mezzanine).

What is the problem?

UPDATE

Running the development server with --insecure parameter solved the problem.

Thanks to GDorn

IF DEBUG = False THEN python manage.py runserver --insecure

Community
  • 1
  • 1
Shojajou
  • 195
  • 6
  • 15

1 Answers1

3

Django does not serve static files if DEBUG=False. The --insecure option seems to be a workaround or more precisely, the dev server runs automatically insecure if in debug mode.

The proper way is in a production environment would be to run python manage.py collectstatic and point the remote proxy server (nginx, apache, or the like) to the resulting folder (which can be configured in settings). Alternatively you can place your statics in an AWS S3 bucket.

You should not run your project of python manage.py runserver in production anyways. If you are not deploying yet, just test your project with DEBUG=True.

An additional option is to add a static view to your url configuration.

# urls.py

from os import path
from django.conf import settings
from django.conf.urls import url
from django.views.static import serve

urlpatterns = [
    # your patterns here 
]

urlpatterns += [url(
    r'^static/(?P<path>.*)$', serve, 
    {'document_root': path.join(settings.BASE_DIR, 'static/public')})]

Obviously the paths have to match your projects. But even this approach is not recommended in production. It just circumvents the fact that Django does not serves static if DEBUG = False. It still requires that you run python manage.py collectstatic whenever you make changes to your static files.

Falk Schuetzenmeister
  • 1,497
  • 1
  • 16
  • 35
  • Thanks. And what is AWS S3? – Shojajou Jun 09 '16 at 06:59
  • 1
    Never mind. It is just a storage solution in the cloud using Amazon Web Services that becomes more and more popular. The case in point is that static files can be collected anywhere since Django doesn't do anything with them. They will be loaded by the clients browser. Of course rendered templates need to point to the correct location. For this reason you need to specify this locations in the settings. – Falk Schuetzenmeister Jun 09 '16 at 16:23