[PYTHON] Notes on creating static files in Django

Try using static files in Python

Try using files such as CSS and Js files in Python.

Set Nginx files

First of all, my server is a combination of Nginx + Gunicorn, so Set the distribution of Nginx

Create an address alias for / static

   location /static {
       alias /app/sampleProj/sampleProj/static;
   }

Create a Django Static route

Then set up a static folder in Django Edit setting.py in the app

setting.py


STATIC_ROOT = os.path.join(BASE_DIR, 'sampleProj/static')
STATIC_URL = '/static/'

** * BASE_DIR is a constant set by Django. It will be the directory where manage.py exists ** You can set it with a relative path from the base directory.

Set Django's static configuration command

By default, Django sets static files in the Django installation folder installed by pip. So type the command to make the current folder a static file

python manage.py collectstatic

Complete with this! Sexual files such as CSS and JS have been set up in Django

Bonus

You can check the added static file directory with the following command!

python manage.py findstatic .

Reference URL

Getting Started with Django | Settings for reading static files with Nginx https://dot-blog.jp/news/django-nginx-static-load/ Easy-to-understand explanation of BASE_DIR [This will give you an idea of the location and structure] https://codor.co.jp/django/about-basedir

Recommended Posts

Notes on creating static files in Django
Celery notes on Django
[Django] Notes on using django-debug-toolbar
About handling Django static files
How to reference static files in a Django project
Notes on imshow () in OpenCV
django notes
Django notes
Find files like find on linux in Python
Notes on nfc.ContactlessFrontend () for nfcpy in python
Notes on using code formatter in Python
Manage Django images and static assets on Ubuntu
Models in Django
[Django] as_view () notes
Notes on Flask
Notes on using dict in python [Competition Pro]
Django Template notes
Designing URL schemes and creating templates in Django
The story of viewing media files in Django
ABC125_C --GCD on Blackboard [Notes solved in Python]
Configure a module with multiple files in Django
Forms in Django
[Django] JWT notes
Learning notes for the migrations feature in the Django framework (2)
Notes on transactions in the Java client library in the datastore
Looking back on creating a web service with Django 1
Notes on creating a python development environment on macOS Catalina
Looking back on creating a web service with Django 2
Learning notes for the migrations feature in the Django framework (3)
Learning notes for the migrations feature in the Django framework (1)
How to upload files in Django generic class view
Miscellaneous notes about deploying the django app on Heroku
Notes on creating a virtual environment with Anaconda Navigator
Notes on writing config files for Python Note: configparser
Upload files with Django
Notes on neural networks
Run Django on PythonAnywhere
Notes on installing PycURL
[Personal notes] Python, Django
Model changes in Django
Hello World on Django
Wget files on OneDrive
[Day 6] Handle static files
Notes on using Alembic
Notes on SciPy.linalg functions
How to handle static files when deploying to production with Django
Notes on how to use marshmallow in the schema library
Notes on coloring by value in the matplotlib scatter plot
Notes on reading and writing float32 TIFF images in python
When I put Django in my home directory, I was addicted to static files with permission errors