Instructions for setting up Django projects.
Table of Contents
- General Setup for all Django Projects
- Django REST Framework
- Django GraphQL
- Django MongoDB
- Full-Stack Django (with templates)
- Django 3.x
- Poetry
- Postgres
- Must have Python 3, Django, and Postgres version 12.x installed
- Make sure Postgres is running on your machine
django-admin startproject [projectname]
- Create a virtual environment:
python -m venv venv
- Go into your virtual environment:
source venv/bin/activate
- Run
poetry init
-> This will create a TOML file for you with your project config where Poetry will add your dependencies - Install
psycopg2-binary
:poetry add psycopg2-binary
- Create a folder named
apps
- Create an
__init__.py
file inside of theapps
folder
- Create a
users
app:mkdir apps/users
and thenpython manage.py startapp users apps/users
- Setup custom User model and custom user manager: https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#a-full-example
AUTH_USER_MODEL = 'users.User'
- Set up admin interface for User model:
from django.contrib import admin
from .models import User
class UserAdmin(admin.ModelAdmin):
list_display = ('email', 'is_admin')
admin.site.register(User, UserAdmin
Postgres is optional, but recommended in the official Django docs.
- Setup Postgres in Django settings.py file:
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '[dbname]',
'USER': '[dbadmin]',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '',
}
- Setup Database in Postgres
- Create the database:
CREATE DATABASE [dbname];
- Create DB user: `CREATE USER [dbadmin];
- Grant privilages to user for our database:
GRANT ALL PRIVILEGES ON DATABASE coducat TO [dbadmin];
- Run migrations:
python manage.py migrate
- Create the database:
- Create an admin user for logging into the Django admin interface:
python manage.py createsuperuser
- Run the app:
python manage.py runserver
- View the API at
localhost:8000
and the admin interface atlocalhost:8000/admin
This builds off of the general Django setup steps.
- Django REST Framework
- Install the REST Framework with Poetry:
poetry add djangorestframework
- Set up Django REST Framework
- Add DRF to
INSTALLED_APPS
:'rest_framework'
- Add DRF URLs to urlpatterns:
path('', include('rest_framework.urls')),
- Add DRF to
- You can run the Django app normally:
python manage.py runserver
- Now go to localhost:8000 in your browser and you should see Django REST frameworks default page showing you all the routes you have available to you.
- Create apps
- Create Models
- Setup Admin interface
- Create
urls.py
file - Setup URLs
- Setup Views
- Setup token auth
- Setup nested routes:
rest_framework_nested
- Pagination
- Timestamp util for models
- MongoDB (Running locally)
- Djongo