Make a note of what to do if you stop reading CSS and image files after creating a Django app according to the tutorial and deploying it in a production environment.
This slide is easy to understand, so it might be better to read this https://tell-k.github.io/djangocongressjp2019/#43
Django handles static files differently during development and in production. To switch this is DEBUG = True / False in setting.py.
Since django is designed to be developed by dividing it into folders for each application, CSS and image files, which are static files, are also placed in the application folder. Therefore, while DEBUG = True, which means under development, all folders named / static / under the folder specified by STATIC_URL and STATICFILES_DIRS written in setting.py are handled by Alias called / static /.
For example, if you have a file like project / app1 / static / base.css It treats it as if it were http://hoge.com/static/base.css.
If you use this function in the production environment, the main process will be unnecessarily overloaded and it may cause vulnerabilities, so you have to turn off this function in the production environment. If you deploy a Django app with Apache2 etc. to force this, only the sample screen will be displayed unless DEBUG = False.
When DEBUG = True, Django provided a static file with an Alias of / static /, but when DEBUG = False, it will not be provided, so in the previous example http://hoge.com/static/ This means that base.css does not exist. If you provide this with Apache etc., you can get the same operation as during development.
Collect static files in one place with python manage.py collectstatic and write the collected location in apahce2.conf so that Apache2 will deliver it as / static /. It's the same as the Django Girls Tutorial .
Create a folder called static directly under the project as shown below and collect it.
proj1 ├── manage.py ├── mysite │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── myvenv │ └── ... ├── static Where to collect static files │ ├── hoge.jpg │ └── base.css └── requirements.txt
Specifies where to collect static files (STATIC_ROOT). In the following, / home / xxxx / django / proj1 / static / will be STATIC_ROOT.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Executing the collectstatics command will collect static files in the PATH of STATIC_ROOT.
cd /home/xxxx/django/proj1 python manage.py collectstatics
I want to treat /home/xxxx/django/proj1/static/base.css as http://hoge.com/static/base.css, so add Alias to the folder and allow it (Require all granted).
Alias /static/ /home/xxxx/django/proj1/static/ <Directory /home/xxxx/django/proj1/static> Require all granted </Directory>
I changed the settings, so restart Apache2.
sudo /etc/init.d/apache2 restart
Since collectstatics simply copies the file, you can check the operation by checking if the file is copied to STATIC_ROOT.
If you haven't successfully registered Apache2, you should see Django wsgi Apache2:'AH01630: client denied by server configuration' in the Apache2 error log. Make sure the contents of apache2.conf are the same as STATIC_ROOT.
If you keep the default settings, you can see the Apache2 error log below.
tail -f /var/log/apache2/error.log