[PYTHON] [Django] css in the project cannot be read

Event

I accessed the Django management site in my local development environment, The css (static file) could not be read on the management site as shown below.

django_admin_no_css_login.png django_admin_no_css.png


Prerequisites

Python 3.7.4
Django 2.2.6
virtualenv 16.1.0

analysis

HTTP log

$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
November 02, 2019 - 16:45:33
Django version 2.2.6, using settings 'studysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[02/Nov/2019 16:24:55] "GET /admin/ HTTP/1.1" 200 3080
[02/Nov/2019 16:24:55] "GET /static/admin/css/responsive.css HTTP/1.1" 404 77
[02/Nov/2019 16:24:55] "GET /static/admin/css/dashboard.css HTTP/1.1" 404 77
[02/Nov/2019 16:24:55] "GET /static/admin/css/base.css HTTP/1.1" 404 77

Apparently css can't be read. I checked settings.py to find out the cause.

settings.py


# snip

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

# snip

The above DEBUG = False was suspicious, so if you set it to True, css can be read. DEBUG = False is supposed to be a production environment, and it seems that static files are read from a web server such as nginx.

counter-measure

In order for css to be loaded, it is necessary to support ** either ** of the following two.

1. Set DEBUG = True in settings.py

If set to True, css in the project will also be loaded.

2. Use the python manage.py runserver --insecure command

With the --insecure option, the css in your project will be loaded.

bonus

settings.py


DEBUG = False
ALLOWED_HOSTS = ['*']

Css was not applied with this setting. It seems that css cannot be read just by setting ALLOWED_HOSTS arbitrarily.

Recommended Posts

[Django] css in the project cannot be read
Django cannot be installed in the development environment of pipenv + pyenv
Project cannot be created with Python3.5 (Windows) + django1.7.1
[Django] Rename the project
Register your Django application in your project
How to reflect CSS in Django
Switch the language displayed in Django 1.9
The meaning of ".object" in Django
Get the query string (query string) in Django
Jupyter Notebook 6.0.2 cannot be installed in the Python 2.7 environment created in Anaconda
Get the client's IP address in Django
Solves the problem that static files (CSS, JS, img) are not read when DEBUG = False in Django.
Django ~ Let's display it in the browser ~
CSS environment created in 10 minutes using Django
Read all csv files in the folder
Read the file line by line in Python
The story of using mysqlclient because PyMySQL cannot be used with Django 2.2
Try hitting the Spotify API in Django.
[Python] Read the specified line in the file
Create a Django project and application in a Python virtual environment and start the server
The _authenticate_with_backend function was obsolete in django auth.autenticate
Publish django project developed in Cloud9 on heroku
Read the output of subprocess.Popen in real time
PyQtGraph may not be available in the interpreter.
Matplotlib etc. cannot be imported in VirtualEnv environment
The problem that the ifconfig command cannot be used
Django Project Baseline
Forms in Django
The avi file output by OpenCV cannot be played
The background color of the QWidget subclass cannot be set
Read the csv file and display it in the browser
[Memorandum] Japanese keys cannot be used in python string.Template.substitute
[Django] Perform Truncate Table (delete all data in the table)
Set the form DateField to type = date in Django
Read the linked list in csv format with graph-tool
Learning notes for the migrations feature in the Django framework (3)
list comprehension because operator.methodcaller cannot be used in python 2.5
Operators ++,-cannot be used in python (difference from php)
Learning notes for the migrations feature in the Django framework (1)
How to reference static files in a Django project
Kaggle Tutorial Titanic know-how to be in the top 2%
Read "Quantum computer made in 14 days". the 2nd day
[Redash] Standard library cannot be used in python function
Excel sheet cannot be specified in sheet_name of pd.read_excel
[Django] Field names, user registration, and login methods that can be used in the User model
Dealing with the error that HTTP fetch error occurs in gpg and the key cannot be obtained