Deploying Django I.Even before we would deploy our Django app to production mode, we have to install some packages and update some files. You should consider possible issues ranging from data models to configuration before deploying. I am introducing here step by step how to prepare a Django app just before deploying it. (Part II.)
Is virtual environment running? If it is not: $ pipenv shell
$ pip install whitenoise
"With a couple of lines of config WhiteNoise allows your web app to serve its own static files, making it a self-contained unit that can be deployed anywhere without relying on nginx, Amazon S3 or any other external service. (Especially useful on Heroku, OpenShift and other PaaS providers.)" https://pypi.org/project/whitenoise/
$ python manage.py collectstatic
https://pypi.org/project/gunicorn/
$ pip install gunicorn
Create Procfile in the project folder without any file extension!
web: gunicorn config.wsgi --log-file -
Change the default sqlite3 database to Postgres!
You can still use the sqlite3 database for local development but you can choose for this purpose postgres too. It is actually up to you. My choice is postgres locally as well. If you are more familiar with database other than postgres you can choose another: https://docs.djangoproject.com/...
To install and set up your postgres, read this!
$ pip install psycopg2
Psycopg2 is a postgres database adapter for Python. Learn more: https://pypi.org/project/psycopg2/.
$ pip install dj-database-url
https://pypi.org/project/dj-database-url/
After you have installed these successfully, you have to add some lines of code to your config/settings.py file. The code below is about database connection for production (Heroku) and also for local.
https://pypi.org/project/environs/
$ pip install environs
Modify your config/settings.py file!
If you set DEBUG to False, you and your visitors will not see any detailed debug information. Hide any information about errors is necessary in production, but in your local development environment you should see that in order to detect the bugs and keep your app secure.
Modify your variables in config/settings.py related to database credentials!
You should define constants in order to set up credentials for your local database. Then set up default values for production!
Create .env file for environment variables then add your datas!
You should add .env file to your .gitignore file so your sensitive data will not appear either on github nor on the server. You can access these data only locally.
If you need a new security key for some reason, (e.g.) you have a django skeleton app just cloned from github. You can generate a new one with this command:
$ python -c 'import secrets;print(secrets.token_urlsafe())'
You need to create the database 'mysite'! Check this!
You should run migrate command to create tables.
export DEBUG=True
# Secret key from config/settings.py
export SECRET_KEY=xxxxxxxx
export DB_NAME=mysite
export DB_PASSWORD=xxxxxxxx
$ pip freeze > requirements.txt
We have just created a text file listed all installed packages.
asgiref==3.5.0
dj-database-url==0.5.0
Django==4.0.1
environs==9.5.0
gunicorn==20.1.0
marshmallow==3.14.1
psycopg2==2.9.3
python-dotenv==0.19.2
sqlparse==0.4.2
tzdata==2021.5
whitenoise==5.3.0