Starting up a Django api can be time consuming. This template is meant to get up and running fast. Once all the wiring is done, you can make modifications to your models and tests as needed.
- poetry
- Django
- djangorestframework
- djangorestframework-simplejwt
- django-cors-headers
- django-environ
- gunicorn
- Docker
- ElephantSQL
- whitenoise
- Black
- psycopg2-binary
cd into your repo
poetry shell
poetry install
python -c 'from django.core.management.utils import get_random_secret_key; \
print(get_random_secret_key())'
4. Edit .env.txt file inside a_project folder with created Django Secret Key and Database info from Elephant Sql
SECRET_KEY=django-insecure-REPLACE_THIS_AREA_WITH_DJANGO_SECRET_KEY
DEBUG=False
ALLOWED_HOSTS=0.0.0.0,127.0.0.1,localhost
ALLOW_ALL_ORIGINS=True
ALLOWED_ORIGINS=http://localhost:3000
DATABASE_ENGINE=django.db.backends.postgresql
DATABASE_NAME= **Get from Elephant sql**
DATABASE_USER= **Get from Elephant sql**
DATABASE_PASSWORD= **Get from Elephant sql**
DATABASE_HOST= **Get from Elephant sql**
DATABASE_PORT=5432
- remember to rename .env.txt to .env
- rename "xxxxx" folder to your new app name(use lower case). Example: xxxxx -> blog
- Do a global search of "Xxxxx" and replace all occurences with your app name
- (For this step, app name needs to be a capital word) Example: Xxxxx -> Blog
- Make sure to select "Match Case" option. [Aa]
- (There will only be 29 matches. If you see 44, you didn't check your case)
- Do a global search of "xxxxx" and replace all occurences with your app name
- (For this step, app name needs to be a lower cased) Example: xxxxx -> blog
- Make sure to select "Match Case" option. [Aa]
- (There will only be 15 matches. If you see a differnt number, you didn't check your case)
poetry export -f requirements.txt -o requirements.txt --without-hashes
python manage.py collectstatic
- make sure docker is running on your computer
docker-compose up
docker-compose run web python manage.py makemigrations
docker-compose run web python manage.py migrate
docker-compose run web python manage.py createsuperuser
docker-compose -d
docker-compose up
docker-compose up --build
docker-compose down
docker-compose stop
docker-compose start
docker-compose restart
docker-compose logs
# This signs us in and grabs access and refresh tokens
http POST :8000/api/token/ email='admin@gmail.com' password='admin'
# This adds our access token to the headers in our request. Gets all blogs
http GET :8000/api/v1/blog/ 'Authorization: Bearer ADD_ACCESS_TOKEN_HERE'
# Gets One Item with pk=4
http GET :8000/api/v1/blog/4 'Authorization: Bearer ADD_ACCESS_TOKEN_HERE'
# Deletes One Item with pk=5
http DELETE :8000/api/v1/blog/5 'Authorization: Bearer ADD_ACCESS_TOKEN_HERE'
This project makes use of a custom user that over writes django's default user model. When you create new models that need to reference user as a foreign key, you need to make the following changes:
# Old Way
from django.contrib.auth import get_user_model
class Post(models.Model):
author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
# ----------------------------------- New Way -----------------------------------
from django.conf import settings
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
In your tests, you would still use get_user_model()