Giter Site home page Giter Site logo

todoapp's Introduction

TODO APP

Softwares Required

This project is about creating a REST API using django, postgres and implementing CRUD operations.

For using this api you have to install following softwares

  1. Python
  2. VSCode-You can use any other editor
  3. Postman- If you have experience with frontend technologies you can integrate it, but here I used postman just to check whether our API is working fine or not.
  4. Postgres- I used Postgres Database, you can use any other Relational Database.
  5. pgadmin- It is a Database Management system for postgres.

Libraries Required

Following are the libraries that you would require

pip install virtualenvwrapper-win
pip install django
pip install psycopg2
  • Creating a virtual Environement
  • Setting up an app
  • Object-Realtional Mapper
  • Setting up urls and views

Creating a virtual Environment

Before understanding how Django works it is important to understand why are we creating a virtual environment. Creating a virtual environement is another way of having a completely independent system unrelated to the versison of libraries that are previously installed in our computer. It helps us to deploy projects in isolation. We can create a virtual environment myfirstvenv using virtualenvwrapper assuming you have already installed python and pip manager:-

mkvirtualenv workon myfirstvenv

To activate myfirstenv

myfirstvenv\Scripts\activate

To deactivate it

deactivate

Setting up an app

Use console to make todos app

python manage.py startapp todos

After this you have to add todos inside INSTALLED_APPS under settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
     #adding application here
    'todos'
]

Object-Relational Mapper

Django uses the concept of Object-Relational Mapper where using Python we make SQL queries. Here by adding following class we are creating a Todo Schema in our database with content as an attribute. Below we are importing models which will help us to create a table and specify its column name along with many other options of giving a default value or make it compulsory for a user to enter that value using blank=false argument.

from django.db import models
class Todo(models.Model):
    content =models.TextField()

Finally we have to tell django that we have made changes in our schema. We always use below commands whenever we are making changes in our schema.

python manage.py makemigrations
python manage.py migrate

Setting up urls and views

Before diving into below code, we have to know what are views. Python allows us to define views that handle specific request and return a response whenever following urls are hit. We have here three urls(all should be distinct), every url corresponds to a view(can be same or different).

from django.urls import path
from . import views

urlpatterns =[
    path('list/',views.list_todo_items),
    path('insert_todo/',views.insert_todo_item,name='insert_todo_item'),
    path('delete_todo/<int:todo_id>/',views.delete_todo_item,name='delete_todo_item'),
]

views.py handles all the request and response and is basically the heart of our application. The following view helps us to add an object in our database. We are using the POST method to add object to our databse and redirecting a webpage ('/todos/list/') as a response.

def insert_todo_item(request: HttpRequest):
    todo = Todo(content=request.POST['content'])
    todo.save()
    return redirect('/todos/list/')

All thanks to CodeAffection

todoapp's People

Contributors

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