Build a To-Do app using CockroachDB and a language/ORM of your choice!
To install CockroachDB go to Cockroach Labs' webpage and follow the instructions.
Go to Cockroach Lab's webpage to learn how to start up an insecure local cluster.
Click for more details
-
Set up and activate a virtual environment:
python3.8 -m venv <name> source <name>/bin/activate
-
Install Django:
python3.8 -m pip install django==3.0.*
-
Install one of the
psycopg2
prerequesites ( psycopg2-binary for this project) -
Install the CockroachDB Django backend:
python -m pip install django-cockroachdb==3.0.*
-
Open a SQL Shell:
cockroach sql --insecure --host=localhost:26257
Make sure your CockroachDB cluster is running before you open the SQL Shell
-
In the SQL Shell enter the following commands:
CREATE USER IF NOT EXISTS django; CREATE DATABASE tododb; GRANT ALL ON DATABASE tododb TO django; \q
-
Run the following command to create a new project: cockroach_todolist for this to-do app.
django-admin startproject cockroach_todolist
This creates a cockroach_todolist
directory. Inside this directory there is a subdirectory cockroach_todolist
and a manage.py
file.
- Open the
settings.py
file incockroach_todolist/cockroach_todolist
and make some modifications:
-
ALLOWED_HOSTS
:ALLOWED_HOSTS = ['0.0.0.0']
-
DATABASES:
DATABASES = { 'default': { 'ENGINE': 'django_cockroachdb', 'NAME': 'tododb', 'USER': 'django', 'HOST': 'localhost', 'PORT': '26257', } }
-
Open the
cockroach_todolist
directory that contains manage.py -
Run the following command:
python manage.py startapp listapp
This creates a listapp
directory in the outer coackroach_todolist
directory.
-
Go to
cockroach_todolist/cockroach_todolist
and modify thesettings.py
file. Add listapp to the INSTALLED_APPS list:INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'listapp' ]
-
Go to the outer
cockroach_todolist
directory (where you can find the manage.py file) and enter the following:python manage.py migrate
To build the web app follow these steps:
-
Define the model for the app in
cockroach_todolist/listapp/models.py
-
Define the URLs needed for the project:
- Open the urls.py file in
cockroach_todolist/cockroach_todolist
and modify it to include the urls needed for the app.
urlpatterns = [ path('admin/', admin.site.urls), path('', include('listapp.urls')) ]
- Create a urls.py file in
cockroach_tolist/listapp
and add the paths to the views.
- Open the urls.py file in
-
Open the views.py file in cockroach_todolist/listapp and write the code for your views.
-
Create a forms.py file in cockroach_todolist/listapp and add the code for the forms.
-
Define the templates:
- Create a
templates
directory inlistapp
. Inside templates create another directory calledlistapp
.
The path to the files looks like this:
cockroach_todolist/listapp/templates/listapp
- Inside create the following files:
indexapp.html
,edit.html
- Create a
-
Go to the
cockroach_todolist
directory where you can finde manage.py. Save the changes to the database:python manage.py makemigrations listapp python manage.py migrate
-
Make sure that the database is set up properly:
-
Open a SQL Shell (check that the cluster is running):
cockroach sql --insecure --host=localhost:26257
-
Enter the following commands:
USE tododb; SHOW TABLES; \q
-
-
Repeat step 2 from Set up the app
-
Add data to the table using
INSERT
statements:SELECT * FROM listapp_taskstodo; INSERT INTO listapp_taskstodo (text, complete, date_added) VALUES ('Cockroach Uni Courses', 'False', NOW()); SELECT * FROM listapp_taskstodo;
-
Retrieve information from the table using
SELECT
statements:SELECT * FROM listapp_taskstodo; SELECT text FROM listapp_taskstodo; SELECT text FROM listapp_taskstodo ORDER BY date_added DESC;
-
In the terminal go to the outer
cockroach_todolist
directory (where you can see manage.py) -
Enter the following command:
python manage.py runserver 0.0.0.0:8000
-
Open browser and paste http://0.0.0.0:8000/
-
Start using the app!!
Copyright [2020] [Carolina Oviedo]
Licensed under the Apache License, Version 2.0