jeff jeff - 1 year ago 102
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 setting.py, I have set,

DEBUG = False
ALLOWED_HOSTS = ['12.10.100.11', '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 manage.py collectstatic


to copy all the static files to a local serving directory.
(following: https://djangobook.com/serving-files-production/)

I then point static url directory to,

STATIC_URL = ("/static/")


I then run,

python manage.py runserver 12.10.100.11:8000


However, on console inspect the error shows:

**Get http://12.10.100.11:8000/static/css/main.css 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

Jef

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