0
votes

I am trying to run the app with DEBUG=False

Below is my setting file configuration

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, "static/")]

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')

After running python manage.py collectstatic, all the static files in the app path are copied to staticfiles directory (mentioned in STATIC_ROOT path).

When loading the webpage, the static files are failed to get load.

Error message:

GET /static/dist/bootstrap-4.0.0-dist/js/bootstrap.min.14d449eb8876.js HTTP/1.1" 404 77

GET /static/dist/bootstrap-select/js/bootstrap-select.min.31f649694651.js HTTP/1.1" 404 77

GET /static/js/base.1332bbb46ac5.js HTTP/1.1" 404 77

GET /static/crsummary/dist/amcharts4/core.ea1ec0eb6727.js HTTP/1.1" 404 77

Looking at the error message, apps is trying to load the bootstrap.min.14d449eb8876.js from path /static/\*/\* but the actual file location is staticfiles/\*/\*

I am not sure what configuration that I have missed out here.

2
Which webserver you're using? Nginx or Apache? It could help if you can provide the configuration for the webserver - Toan Quoc Ho
Where are the static files when you're not in production? Are they not in a folder called static/? python manage.py collectstatic simply takes all of your static files and puts them into one folder (staticfiles/ in your case), but it doesn't mean they will be served from there. As @ToanQuocHo asked, which server are you using? - Sam Creamer
I haven't used any external web servers like Apache, but I tried using development server itself (provided by Django) to test if DEBUG=False works. I have given my computer's IP address in ALLOWED_HOST. Will this error occur if we do not use any external web servers while deploying Django in production ? - Ramdinesh J P
It won't work as your expected, and it's very not good for your app when running using runserver command. If you still want to follow this way, make sure that your static url config on your urls.py is not stay inside if settings.DEBUG: condition block. But I really not recommend you to even try this. - Toan Quoc Ho
I think you have to set DEBUG=True when running in runserver. Please read docs.djangoproject.com/en/2.2/howto/static-files - elomat

2 Answers

1
votes

Django doesn't allow to load staticfiles when codes in production, instead of using aws or any other online content delivery network

If you really wan't to load staticfiles from the codes directory , you need to install whitenoise , and define it in your middleware, staticfiles, and installed_apps

for more you can search for use and settings whitenoise in django

0
votes
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href=" {%static 'app1/css/freelancer.css' %}" />

Load the static files like this.

{% load static %}
<img class="img-fluid" src="{% static 'app1/img/portfolio/ai.png' %}" alt="">

Load the image like this.

Before that , Required to specify the Base directory and static directory

STATIC_DIR=os.path.join(BASE_DIR,"static")

STATIC_URL = '/static/'
STATICFILES_DIRS=[STATIC_DIR,]

Base directory

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Build paths inside the project like this: os.path.join(BASE_DIR, ...)