jeff jeff - 1 year ago 90
Python Question

Setup static file in Django from serving site in production mode

I am trying to run my static files from the same serving site in production mode. This is what I did.

In, I have set,

DEBUG = False
ALLOWED_HOSTS = ['', 'localhost']

I included, 'django.contrib.staticfiles' in INSTALLED_APPS

I set the Static root directory to,

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")

I ran,

python collectstatic

to copy all the static files to a local serving directory.

I then point static url directory to,

STATIC_URL = ("/static/")

I then run,

python runserver

However, on console inspect the error shows:

**Get 404 (not found)**

on my base.html looks something like this:

{% load static %}
<link rel="stylesheet" href="{% static '/css/main.css' %}" type="text/css" />

I am new to Django and need lots of advice, thanks


Answer Source

You can't serve static files. django with DEBUG mode turned off isn't responsible for that. Ngnix is better option with chache mechanisms for that. You should check out Heroku platform how they deploy production ready django projects with whitenoise for example. Github has a lot of examples of opensource projects powered with django that you can learn from

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download