Boilerplate Django Blog for production deployments!

Related tags

Djangocfe-django-blog
Overview

CFE Django Blog

THIS IS COMING SOON

This is boilerplate code that you can use to learn how to bring Django into production.

TLDR;

This is definitely coming soon -- basically a list of all commands to get this repo working locally.

Getting Started

Step 1: Fork / Clone

First, decide where this project will live.

mkdir -p ~/dev
cd ~/dev

I always use the ~/Dev folder which translates to /users/cfe/Dev (macOS / Linux) or C:\Users\cfe\Dev (windows). This location is optional.

Fork or clone this repo:

git clone https://github.com/codingforentrepreneurs/cfe-django-blog

Step 2: Create a Virtual Environment

Isolate your python project from other python projects by using the built-in venv module:

python3.10 -m venv venv
  • I recommend Python 3.8 and up
  • You can use any virtual environment manager (poetry, pipenv, virtualenv, etc)

Step 3: Activate Virtual Environment

macOS/Linux

source venv/bin/activate

Windows

.\venv\Scripts\activate

Step 4: Install Requirements

$(venv) python -m pip install pip --upgrade
$(venv) python -m pip install -r requirements.txt
  • $(venv) is merely denoting the virtual environment is activated
  • In requirements.txt you'll see django>=3.2,<4.0 -- this means I'm using the latest version of Django 3.2 since it's an LTS release.
  • You can use venv/bin/python -m pip install -r requirements.txt (mac/linux) or venv\bin\python -m pip install -r requirements.txt (windows)
  • pip install ... is not as reliable as python -m pip install ...

Step 5: Select a Database

As of now, we have the following supported databases for this boilerplate code: sqlite, mysql, postgres

sqlite

No action needed. Django will managed sqlite for you.

mysql

To install Python client:

$(venv) python -m pip install mysqlclient

If macOS, you must run brew install mysql (assuming you have homebrew installed)

Be sure you add mysqlclient to requirements.txt like:

echo "mysqlclient" >> requirements.txt

Using the double >> vs a single > is the difference between appending and overwriting respectively.

postgres

$(venv) python -m pip install psycopg2

You may need to use python -m pip install psycopg2-binary during development.

Be sure you add psycopg2 to requirements.txt like:

echo "psycopg2" >> requirements.txt

Using the double >> vs a single > is the difference between appending and overwriting respectively.

Step 6: Setup your .env

Create your .env file.

echo "" > .env

Below is an example of development-ready .env file for this project. ALWAYS update these values when going into production.

# required keys
DJANGO_SECRET_KEY=gy_1$n9zsaacs^a4a1&-i%e95fe&d3pa+e^@5s*tke*r1b%*cu
DATABASE_BACKEND=postgres

# mysql db setup
MYSQL_DATABASE=cfeblog-m-db
MYSQL_USER=cfeblog-m-user
MYSQL_PASSWORD=RaSNF5H3ElCbDrGUGpdRSEx-IuDzkeHFL_S_QBuH5tk
MYSQL_ROOT_PASSWORD=2mLTcmdPzU2LOa0TpAlLPoNf1XtIKsKvNn5WBiszczs
MYSQL_TCP_PORT=3007
MYSQL_HOST=127.0.0.1

# postgres db setup
POSTGRES_DB=cfeblog-p-db
POSTGRES_USER=cfeblog-m-user
POSTGRES_PASSWORD=NwgFCimzL0Oqd539EYzsztY04uzw2jaVEIrH1OK2sz0
POSTGRES_PORT=5431
POSTGRES_HOST=localhost

To generate secrets use one of the following method(s):

Use Django to create a one-off secret key (bookmark this blog post):
python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'

This is the recommended method for creating the DJANGO_SECRET_KEY

Use Python to create a url safe secret:
python -c "import secrets;print(secrets.token_urlsafe(32))"

Step 6: Local Development with Docker Compose

Running our local environment with the same type of database as our production database is critical. For this, we'll use Docker and Docker Compose.

With Docker, your machine can have a lot of instances of MySQL/Postgres/Redis running with minimal configuration. This is true for macOS, Windows, and nearly all distros of Linux that can run Docker.

Without Docker, having more than 1 version of any of these running is a huge pain. A pain that might be worth going through if you like to bleed from your eyes. It's also an uncessary pain because we're talking about the development environment.

Now on to the nitty gritty.

In our docker-compose.yaml file, you'll see configuration for the services:

  • mysql_db
  • postgres_db
  • redis_db

But wait, there is not a web service for Django in docker-compose.yaml... why not? Two reasons:

  • If you need it, you can add it.
  • If you're new to Python, Virtual Environments, Django, Docker, VSCode, Git, or whatever it makes things even more complex.

If you're new to Docker compose, this might suck too. Sorry about that. But I hope you trust me that, in this case, the juice is worth the squeeze (aka it's worth the effort in learning how to use it).

Since I wanted to support both mysql and postgres I wanted to make use of Docker Compose's profiles feature.

Basicaly, you can use a profile to "activate" different services within a Docker compose file (instead of having a bunch of different Docker compose files).

In our case, docker-compose.yaml has three profiles:

  • mysql (includes the mysql_db and redis_db services)
  • postgres (includes the postgres_db and redis_db services)
  • redis (runs only the redis_db service)

To run any given profile you just do:

docker compose --profile mysql up

Just replace --profile postgres if you want to use that one.

Also, keep in mind that some systems require you to use docker-compose instead of docker compose.

I recommend running this profile in background mode (aka detached mode):

docker compose --profile mysql up -d

Again, just replace --profile postgres if you want to use that one.

Owner
Coding For Entrepreneurs
Build real projects and learn to code. Step by step. By @jmitchel3
Coding For Entrepreneurs
Show how the redis works with Python (Django).

Redis Leaderboard Python (Django) Show how the redis works with Python (Django). Try it out deploying on Heroku (See notes: How to run on Google Cloud

Tom Xu 4 Nov 16, 2021
Boilerplate Django Blog for production deployments!

CFE Django Blog THIS IS COMING SOON This is boilerplate code that you can use to learn how to bring Django into production. TLDR; This is definitely c

Coding For Entrepreneurs 26 Dec 09, 2022
Developer-friendly asynchrony for Django

Django Channels Channels augments Django to bring WebSocket, long-poll HTTP, task offloading and other async support to your code, using familiar Djan

Django 5.5k Jan 06, 2023
Add a help desk or knowledge base to your Django project with only a few lines of boilerplate code.

This project is no longer maintained. If you are interested in taking over the project, email Zapier 487 Dec 06, 2022

PEP-484 type hints bindings for the Django web framework

mypy-django Type stubs to use the mypy static type-checker with your Django projects This project includes the PEP-484 compatible "type stubs" for Dja

Machinalis 223 Jun 17, 2022
Location field and widget for Django. It supports Google Maps, OpenStreetMap and Mapbox

django-location-field Let users pick locations using a map widget and store its latitude and longitude. Stable version: django-location-field==2.1.0 D

Caio Ariede 481 Dec 29, 2022
The Django Leaflet Admin List package provides an admin list view featured by the map and bounding box filter for the geo-based data of the GeoDjango.

The Django Leaflet Admin List package provides an admin list view featured by the map and bounding box filter for the geo-based data of the GeoDjango. It requires a django-leaflet package.

Vsevolod Novikov 33 Nov 11, 2022
Application made in Django to generate random passwords as based on certain criteria .

PASSWORD GENERATOR Welcome to Password Generator About The App Password Generator is an Open Source project brought to you by Iot Lab,KIIT and it brin

IoT Lab KIIT 3 Oct 21, 2021
DCM is a set of tools that helps you to keep your data in your Django Models consistent.

Django Consistency Model DCM is a set of tools that helps you to keep your data in your Django Models consistent. Motivation You have a lot of legacy

Occipital 59 Dec 21, 2022
📊📈 Serves up Pandas dataframes via the Django REST Framework for use in client-side (i.e. d3.js) visualizations and offline analysis (e.g. Excel)

Django REST Pandas Django REST Framework + pandas = A Model-driven Visualization API Django REST Pandas (DRP) provides a simple way to generate and se

wq framework 1.2k Jan 01, 2023
Packs a bunch of smaller CSS files together from 1 folder.

Packs a bunch of smaller CSS files together from 1 folder.

1 Dec 09, 2021
Buckshot++ is a new algorithm that finds highly stable clusters efficiently.

Buckshot++: An Outlier-Resistant and Scalable Clustering Algorithm. (Inspired by the Buckshot Algorithm.) Here, we introduce a new algorithm, which we

John Jung 1 Jul 02, 2022
Django server-side adapter for Inertia.js

django-inertia Django server-side new adapter for Inertia.js. Getting Started Install the package pip install django-inertia Configure your project A

Samuel Girardin 14 Sep 16, 2022
An orgizational tool to keep track of tasks/projects and the time spent on them.

Django-Task-Manager Task Tracker using Python Django About The Project This project is an orgizational tool to keep track of tasks/projects and the ti

Nick Newton 1 Dec 21, 2021
Projeto Crud Django and Mongo

Projeto-Crud_Django_and_Mongo Configuração para rodar o projeto Download Project

Samuel Fernandes Oliveira 2 Jan 24, 2022
Dockerizing Django with Postgres, Gunicorn, Nginx and Certbot. A fully Django starter project.

Dockerizing Django with Postgres, Gunicorn, Nginx and Certbot 🚀 Features A Django stater project with fully basic requirements for a production-ready

8 Jun 27, 2022
Send push notifications to mobile devices through GCM or APNS in Django.

django-push-notifications A minimal Django app that implements Device models that can send messages through APNS, FCM/GCM and WNS. The app implements

Jazzband 2k Dec 26, 2022
Coltrane - A simple content site framework that harnesses the power of Django without the hassle.

coltrane A simple content site framework that harnesses the power of Django without the hassle. Features Can be a standalone static site or added to I

Adam Hill 58 Jan 02, 2023
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.

Django-environ django-environ allows you to use Twelve-factor methodology to configure your Django application with environment variables. import envi

Daniele Faraglia 2.7k Jan 07, 2023
Per object permissions for Django

django-guardian django-guardian is an implementation of per object permissions [1] on top of Django's authorization backend Documentation Online docum

3.3k Jan 04, 2023