[PYTHON] How to handle static files when deploying to production with Django

When I was working in django in my local environment and running it in production, the static files disappeared.

How to deal with it.

--Server: nginx

Routing system

nginx.conf


#server{}Added in
    
    location / {
        root /home/hogehoge/hogehoge; #Application directory path on the server
        proxy_pass http://127.0.0.1:8000;
    }

At the time of deployment, I set it like this.

Turn off debug mode

In settings.py, rewrite the following contents.

settings.py


DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1','Any DNS']
STATIC_ROOT = "/home/hogehoge/hogehoge/static" #It seems to specify the path where you want to put the static file
#In addition to this, STATIC FILES_DIRS = {... seems to comment out

If this is left as it is, the static file will be hidden, so

python manage.py collectstatic

Execute. Then rewrite the settings with nginx

nginx.conf


#server{}In

location /static/ {
        alias /home/hogehoge/hogehoge/static/; #settings.Describe the same location set in py
    }

I will write it like this. that's all.

Recommended Posts

How to handle static files when deploying to production with Django
How to reference static files in a Django project
Handle csv files with Django (django-import-export)
How to resolve CSRF Protection when using AngularJS with Django
[Linux] How to deal with garbled characters when viewing files
How to get started with Django
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to deal with errors when installing whitenoise and deploying to Heroku
[Tips] How to do template extends when creating HTML with django
How to do arithmetic with Django template
When I put Django in my home directory, I was addicted to static files with permission errors
How to display Django static files correctly under Amazon Linux2 + Apache environment
How to deal with errors when hitting pip ②
[Django] How to resolve errors when installing mysqlclient
How to develop a cart app with Django
How to deal with SessionNotCreatedException when using Selenium
How to implement "named_scope" of RubyOnRails with Django
[Python] How to handle Japanese characters with openCV
Upload files with Django
[Day 6] Handle static files
How to load files in Google Drive with Google Colaboratory
How to access with cache when reading_json in pandas
When you want to filter with Django REST framework
How to upload files in Django generic class view
Things to do when you start developing with Django
Things to watch out for when migrating with Django
How to not load images when using PhantomJS with Selenium
How to check ORM behavior in one file with django
How to update user information when logging in to Django RemoteUserMiddleware
When I deploy a Django app with Apache2 and it no longer reads static files
How to update with SQLAlchemy?
[Django] How to give input values in advance with ModelForm
How to upload files to Cloud Storage with Firebase's python SDK
How to cast with Theano
How to Alter with SQLAlchemy?
How to separate strings with','
How to RDP with Fedora31
About handling Django static files
Handle JSON files with Matlab
How to handle data frames
How to deal with UnicodeDecodeError when executing google image download
How to Delete with SQLAlchemy?
How to output additional information when logging with python's logging module
How to not escape Japanese when dealing with json in python
How to deal with "You have multiple authentication backends configured ..." (Django)
How to import CSV and TSV files into SQLite with Python
Here's a brief summary of how to get started with Django
How to deal with errors when installing Python and pip with choco
How to cancel RT with tweepy
Steps to develop Django with VSCode
How to handle session in SQLAlchemy
Python: How to use async with
[Django] How to test Form [TDD]
How to use virtualenv with PowerShell
How to deal with imbalanced data
Manage Django config files with Python-decouple
How to install python-pip with ubuntu20.04LTS
Common html to rent with Django
Serve static files with X-Send File
How to deal with imbalanced data