Giter Club home page Giter Club logo

django-to-do-list's Introduction

TO DO LIST APPLICATION

1. Link course

2. Create project

$ python manage.py startproject todo_main .
  • Note: If not have "." end of statement, django will create new folder with same name project and give project inside it

3. Migrate database

$ python manage.py migrate

4. Create supper user

$ python manage.py createsupperuser

5. Create HomeView

  • Step 1: Add path into todo_main/urls.py of project
from django.contrib import admin
from django.urls import path
from . import views # It's here

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'), # It's here
]
  • Step 2: Create file views.py into todo_main
from django.shortcuts import render

def home(request):
    return render(request, 'home.html')
  • Step 3: Create folder templates in root directory (same level with todo_main), create home.html inside it
  • Step 4: Add path of folder templates at field TEMPLATES/DIRS in file settings.py

6. Create app

  • Step 1: Enter command line with name of app as todo
$ python manage.py startapp todo
  • Step 2: Declare app todo at field INSTALLED_APPS in file settings.py
  • Step 3: Define a model Task in todo/models.py
from django.db import models

# Create your models here.
class Task(models.Model):
    task = models.CharField(max_length=250)
    is_completed = models.BooleanField(default=False)

    # Recommend to use for every model, because it's very important while storing the big data in database.
    # Can see when data was created and modified previously
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    # Set a string representation of this model
    def __str__(self):
        return self.task
  • Step 4: Register model inside todo/admin.py
from django.contrib import admin
from .models import Task

# Register your models here.
admin.site.register(Task)
  • Step 5: Enter command line to makemigrate and migrate new model to database (Very important when define new model)
$ python manage.py makemigrations
$ python manage.py migrate
  • Note: If not have this step, it'll return error when click to Task in Admin Page (http://127.0.0.1:8000/admin/). Because cannot find that model.

7. Create logic code to fectch tasks from database and show on UI

  • Step 1: Create incompleted and completed tasks on Admin Page
  • Step 2: Edit logic code in todo_main/views.py and templates/home.html

8. Create TaskAdmin to manage tasks

  • Create class TaskAdmin is inherited from admin.ModelAdmin in file todo/admin.py
from django.contrib import admin
from .models import Task

# Register your models here.
class TaskAdmin(admin.ModelAdmin):
    list_display = ('task', 'is_completed', 'created_at', 'updated_at')
    search_fields = ('task',)

admin.site.register(Task, TaskAdmin)

9. Create add_task function and activate CSRF token

  • Step 1: Define path in todo_main/urls.py
  • Step 2: In todo, create new file urls.py and add an API
  • Step 3: In todo/views.py, create function is called by todo/urls.py
  • Step 4: Change some fields to get data from form
  • Step 5: Activate CSRF token to secure (Very important, always activate CSRF token with POST method)

10. Mark as done

  • Step 1: Add API mark-as-done in todo/urls.py
  • Step 2: In todo/views.py, create function is called by todo/urls.py
  • Step 3: In file templates/home.html, at anchor tag which has href, add name of API here
<a href="{% url 'mark_as_done' incompleted_task.pk %}" class="btn btn-success"><i class="fa fa-check"></i> Mark as Done</a>

11. Edit task

  • Step 1: Add API edit_task in todo/urls.py
  • Step 2: In todo/views.py, create function is called by todo/urls.py
  • Step 3: In file templates/home.html, at anchor tag which has href, add name of API here
  • Step 4: In edit_task function, define 2 logic, one for GET method to get info current task, other for POST method to assign value new task to current task for updating purpose
def edit_task(request, pk):
    task = get_object_or_404(Task, pk=pk)
    if request.method == 'POST':
        updated_task = request.POST.get('updated_task')
        task.task = updated_task
        task.save()
        return redirect('home')
    else:
        context = {
            'task': task
        }
    return render(request, 'edit_task.html', context)
  • Step 5: Create templates/edit_task.html

django-to-do-list's People

Contributors

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