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
A small and lightweight imageboard written with Django

Yuu A small and lightweight imageboard written with Django. What are the requirements? Python 3.7.x PostgreSQL 14.x Redis 5.x FFmpeg 4.x Why? I don't

mint.lgbt 1 Oct 30, 2021
Full control of form rendering in the templates.

django-floppyforms Full control of form rendering in the templates. Authors: Gregor Müllegger and many many contributors Original creator: Bruno Renié

Jazzband 811 Dec 01, 2022
A small Django app to easily broadcast an announcement across a website.

django-site-broadcasts The site broadcast application allows users to define short messages and announcements that should be displayed across a site.

Ben Lopatin 12 Jan 21, 2020
Neighbourhood - A python-django web app to help the residence of a given neighborhood know their surrounding better

Neighbourhood A python-django web app to help the residence of a given neighborh

Levy Omolo 4 Aug 25, 2022
A simple Django dev environment setup with docker for demo purposes for GalsenDev community

GalsenDEV Docker Demo This is a basic Django dev environment setup with docker and docker-compose for a GalsenDev Meetup. The main purposes was to mak

3 Jul 03, 2021
pdm-django: Django command shortcuts for PDM

pdm-django: Django command shortcuts for PDM A plugin that gives you command shortcuts for developing with PDM. pdm run python manage.py runserver -

Neutron Sync 2 Aug 11, 2022
Django Phyton Web Apps template themes

Django Phyton Web Apps template themes Free download source code project for build a modern website using django phyton web apps. Documentation instal

Mesin Kasir 4 Dec 15, 2022
Example project demonstrating using Django’s test runner with Coverage.py

Example project demonstrating using Django’s test runner with Coverage.py Set up with: python -m venv --prompt . venv source venv/bin/activate python

Adam Johnson 5 Nov 29, 2021
A starter template for building a backend with Django and django-rest-framework using docker with PostgreSQL as the primary DB.

Django-Rest-Template! This is a basic starter template for a backend project with Django as the server and PostgreSQL as the database. About the templ

Akshat Sharma 11 Dec 06, 2022
Wrapping Raml around Django rest-api's

Ramlwrap is a toolkit for Django which allows a combination of rapid server prototyping as well as enforcement of API definition from the RAML api. R

Jmons 8 Dec 27, 2021
Django Fett is an incomplete code generator used on several projects

Django Fett Django Fett is an incomplete code generator used on several projects. This is an attempt to clean it up and make it public for consumption

Jeff Triplett 6 Dec 31, 2021
Store events and publish to Kafka

Create an event from Django ORM object model, store the event into the database and also publish it into Kafka cluster.

Diag 6 Nov 30, 2022
Simple reproduction of connection leak with celery/django/gevent

Redis connection leak with celery/django/gevent Reproduces celery issue at https://github.com/celery/celery/issues/6819 using gevented django web serv

2 Apr 03, 2022
Django backend of Helium's planner application

Helium Platform Project Prerequisites Python (= 3.6) Pip (= 9.0) MySQL (= 5.7) Redis (= 3.2) Getting Started The Platform is developed using Pytho

Helium Edu 17 Dec 14, 2022
Utilities to make function-based views cleaner, more efficient, and better tasting.

django-fbv Utilities to make Django function-based views cleaner, more efficient, and better tasting. 💥 📖 Complete documentation: https://django-fbv

Adam Hill 49 Dec 30, 2022
Helps working with singletons - things like global settings that you want to edit from the admin site.

Django Solo +---------------------------+ | | | | | \ | Django Solo helps

Sylvain Toé 726 Jan 08, 2023
Generate generic activity streams from the actions on your site. Users can follow any actors' activities for personalized streams.

Django Activity Stream What is Django Activity Stream? Django Activity Stream is a way of creating activities generated by the actions on your site. I

Justin Quick 2.1k Dec 29, 2022
demo project for django channels tutorial

django_channels_chat_official_tutorial demo project for django channels tutorial code from tutorial page: https://channels.readthedocs.io/en/stable/tu

lightsong 1 Oct 22, 2021
Simpliest django(uvicorn)+postgresql+nginx docker-compose (ready for production and dev)

simpliest django(uvicorn)+postgresql+nginx docker-compose (ready for production and dev) To run in production: docker-compose up -d Site available on

Artyom Lisovskii 1 Dec 16, 2021
A simple plugin to attach a debugger in Django on runserver command.

django-debugger A simple plugin to attach a debugger in Django during runserver Installation pip install django-debugger Usage Prepend django_debugger

Sajal Shrestha 11 Nov 15, 2021