Skip to content

evanshortiss/django-neon-quickstart

Repository files navigation

Python, Django, and Neon's Serverless Postgres

This repository is a companion to a Python & Django with Neon post on the Neon blog.

An example Django application that connects to Neon's serverless Postgres, and uses HTMX for interactions.

Neon's serverless Postgres auto-suspends when the Django application hasn't issued a query for 5 minutes, meaning you get an on-demand database. This is great for development, test, and staging environments that don't need to run 24/7.

Elements Application in Google Chrome

Usage

Optional Environment Setup

Follow the pyenv installation instructions for your OS, then run the following commands to install and use Python 3.12.1:

# This will list existing python versions on your system
pyenv versions

# Install Python 3.12.1 and set it as the version for this terminal session
pyenv install 3.12.1
pyenv local 3.12.1

# Confirm the python version in use is 3.12.1
python --version

Use venv in the same shell to create a virtual environment that uses Python 3.12, and use the source command to activate it:

# This path is an example. You can store the virtual environment wherever you want
python -m venv $HOME/.venv/neon-and-django-env

# Activate the virtual environment in the current shell. Note that fish, powershell,
# and Windows users must use the activate script for their specific shell per
# https://docs.python.org/3/library/venv.html#how-venvs-work 
source $HOME/.venv/neon-and-django-env/bin/activate

Run the Application

Install Project Dependencies

From the root of the project, run:

pip install -r requirements.txt

Define Postgres Connection Details

  1. Visit console.neon.tech and create a new project, or new database in an existing project.
  2. Copy the .env.example file in root of this repository into a new file named .env.
  3. Replace the sample values in .env with the Connection Details from console.neon.tech for your chosen database.

Apply Database Migrations

This will create Django's tables, and tables for the application's Element model.

python manage.py makemigrations
python manage.py migrate

Start the Application

Prior to starting the application, uise the SQL Editor in the Neon Console to add some data to your elements_element table:

INSERT INTO elements_element (name, symbol, atomic_number)
VALUES
('Hydrogen', 'H', 1),
('Helium', 'He', 2),
('Lithium', 'Li', 3),
('Beryllium', 'Be', 4),
('Boron', 'B', 5),
('Carbon', 'C', 6);

Start the application in development mode using python manage.py runserver, and visit http://localhost:8000 to see a list of elements.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published