Giter Club home page Giter Club logo

django-todo-app-cockroachdb-hacktoberfest-challenge's Introduction

Django TO-DO LIST app

Website Website Website Website Website

The Challenge

Build a To-Do app using CockroachDB and a language/ORM of your choice!

Instructions

1. Install CockroachDB

To install CockroachDB go to Cockroach Labs' webpage and follow the instructions.

2. Start a local CockroachDB cluster

Go to Cockroach Lab's webpage to learn how to start up an insecure local cluster.

3. Install Django and the CockroachDB backend for Django

Click for more details

  1. Set up and activate a virtual environment:

    python3.8 -m venv <name>
    source <name>/bin/activate
  2. Install Django:

    python3.8 -m pip install django==3.0.*
  3. Install one of the psycopg2 prerequesites ( psycopg2-binary for this project)

  4. Install the CockroachDB Django backend:

    python -m pip install django-cockroachdb==3.0.*

4. Create the django user and tododb database

  1. Open a SQL Shell:

    cockroach sql --insecure --host=localhost:26257

    Make sure your CockroachDB cluster is running before you open the SQL Shell

  2. 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

5. Create a Django project

  1. 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.

  1. Open the settings.py file in cockroach_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',
        }
    }

6. Create the Django App (Web application)

  1. Open the cockroach_todolist directory that contains manage.py

  2. Run the following command:

     python manage.py startapp listapp

This creates a listapp directory in the outer coackroach_todolist directory.

Click for more details

The project and app directories now look like this:
directories

  1. Go to cockroach_todolist/cockroach_todolist and modify the settings.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'
    ]  
  2. Go to the outer cockroach_todolist directory (where you can find the manage.py file) and enter the following:

     python manage.py migrate 

7. Write the application logic

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 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 in listapp. Inside templates create another directory called listapp.
      The path to the files looks like this:
    cockroach_todolist/listapp/templates/listapp
    • Inside create the following files: indexapp.html, edit.html

8. Set up the app

  1. 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
  2. Make sure that the database is set up properly:

    1. Open a SQL Shell (check that the cluster is running):

      cockroach sql --insecure --host=localhost:26257
    2. Enter the following commands:

      USE tododb;   
      
      SHOW TABLES;  
      
      \q
      

9. Run the app

1. From the command line using the SQL Shell

  1. Repeat step 2 from Set up the app

  2. 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;  
  3. 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;

    sqlfinal

2. Web Application

  1. In the terminal go to the outer cockroach_todolist directory (where you can see manage.py)

  2. Enter the following command:

    python manage.py runserver 0.0.0.0:8000
  3. Open browser and paste http://0.0.0.0:8000/

  4. Start using the app!!

sqlfinal

Twitter Follow


Copyright [2020] [Carolina Oviedo]

Licensed under the Apache License, Version 2.0

django-todo-app-cockroachdb-hacktoberfest-challenge's People

Contributors

caro-oviedo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.