Skip to content

Latest commit

 

History

History
136 lines (97 loc) · 4.3 KB

README.md

File metadata and controls

136 lines (97 loc) · 4.3 KB

Django Setup Wiki

Instructions for setting up Django projects.

Table of Contents

  1. General Setup for all Django Projects
  2. Django REST Framework
  3. Django GraphQL
  4. Django MongoDB
  5. Full-Stack Django (with templates)

General Setup for all Django Projects

Recommended Technologies

  • Django 3.x
  • Poetry
  • Postgres: You can use MySQL or SQL Lite, but Postgres is recommended by the Django official docs. The only exception is if you want to use MongoDB or another NoSQL database with your project. You can find details for that below.

Initial Setup

  1. Must have Python 3, Django, and Postgres version 12.x installed

  2. Make sure Postgres is running on your machine

  3. django-admin startproject [projectname]

  4. Create a virtual environment: python -m venv venv

  5. Go into your virtual environment: source venv/bin/activate

  6. Run poetry init -> This will create a TOML file for you with your project config where Poetry will add your dependencies

  7. Install psycopg2-binary: poetry add psycopg2-binary

  8. Rename the [projectname] folder to config

  9. Create a folder named apps

  10. Create an __init__.py file inside of the apps folder

Users Setup

  1. Create a users app: mkdir apps/users and then python manage.py startapp users apps/users
  2. Make sure you add apps.users to installed apps in the settings.py file
  3. Also, if you are using an apps folder as I recommend in this wiki, you will need to change the name of every Django app in the apps.py file: e.g. from name = users to name = apps.users
  4. 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'
  1. 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

Database Setup

Postgres is optional, but recommended in the official Django docs.

  1. Setup Postgres in Django settings.py file:
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': '[dbname]',
    'USER': '[dbadmin]',
    'PASSWORD': '',
    'HOST': 'localhost',
    'PORT': '',
}
  1. Setup Database in Postgres
    1. Create the database: CREATE DATABASE [dbname];
    2. Create DB user: `CREATE USER [dbadmin];
    3. Grant privilages to user for our database: GRANT ALL PRIVILEGES ON DATABASE coducat TO [dbadmin];
    4. Run migrations: python manage.py migrate

More Setup

  1. Create an admin user for logging into the Django admin interface: python manage.py createsuperuser
  2. Run the app: python manage.py runserver
  3. View the API at localhost:8000 and the admin interface at localhost:8000/admin

Django REST Framework

This builds off of the general Django setup steps.

Technologies

  • Django REST Framework

REST Framework Setup

  1. Install the REST Framework with Poetry: poetry add djangorestframework
  2. Set up Django REST Framework
    1. Add DRF to INSTALLED_APPS: 'rest_framework'
    2. Add DRF URLs to urlpatterns: path('', include('rest_framework.urls')),
  3. You can run the Django app normally: python manage.py runserver
  4. 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.

Apps Setup

  1. Create apps
  2. Create Models
  3. Setup Admin interface
  4. Create urls.py file
  5. Setup URLs
  6. Setup Views

Optional Setup

  1. Setup token auth
  2. Setup nested routes: rest_framework_nested
  3. Pagination
  4. Timestamp util for models

Django Graphql

Technologies

Django MongoDB

Technologies

  • MongoDB (Running locally)
  • Djongo

Full-Stack Django

coming soon...