Giter Club home page Giter Club logo

django-to-do-app's Introduction

django-to-do-app

An introduction into the forays of django

Play by play instructions:

  1. Create a new project
django-admin startproject todo
  1. Start the server
cd todo && python3 manage.py runserver 
  1. Create a new application within the project
python3 manage.py startapp todoapp
  1. Register the new application
installed_apps = [
    ...,
    todoapp,
    ...,
]
  1. Create the template
mkdir templates
cd templates && touch todolist.html
  1. Add boiler play html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    Hey look everyone, i am learning how to code!
</body>
</html>
  1. add a view to views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
# from .models import TodoListItem

# Create your views here.
def todoView(request):
    # all_todo_items = TodoListItem.objects.all()
    return render(request, 'todolist.html', {
        # 'all_items': all_todo_items
    })
  1. add the url to urls.py in the main app
from django.contrib import admin
from django.urls import path
from todoapp.views import todoView#, addTODO, deleteTodoView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('todo/', todoView),
    # path('addTODO/', addTODO),
    # path('deleteTodoItem/<int:i>/', deleteTodoView),
]
  1. add the templates to the base directory
import os
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')], 
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
  1. remove /todo from the endpoint in urls.py
from django.contrib import admin
from django.urls import path
from todoapp.views import todoView#, addTODO, deleteTodoView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', todoView),
    # path('addTODO/', addTODO),
    # path('deleteTodoItem/<int:i>/', deleteTodoView),
]
  1. create a model in models.py in the app (without the str)
from django.db import models

# Create your models here.
class TodoListItem(models.Model):
    name = models.TextField()
  1. makemigrations
python3 manage.py makemigrations
  1. migrate
python3 manage.py migrate
  1. Go to localhost:8000 and you will reallise you don't have the username and password. So create a superuser to see the model.
python3 manage.py createsuperuser
  1. register the model in admin
from django.contrib import admin

# Register your models here.
from .models import TodoListItem
admin.site.register(TodoListItem)
  1. add the str method
class TodoListItem(models.Model):
    name = models.TextField()

    def __str__(self):
        return self.name
  1. Add the adding method

    • Add this into todolist.html
      <form action="/addTODO/" method="post">
          {% csrf_token %}
          <input type="text" name="name">
          <input type="submit" value="Add Item">
      </form>
    • Add the url
      path('addTODO/', addTODO),
    • Add the view
      def addTODO(request):
          x = request.POST['name']
          print(x)
          new_item = TodoListItem(name=x)
          new_item.save()
          return HttpResponseRedirect('/')
  2. Add the viewing of the list by commenting out the stuff from before

    def todoView(request):
        all_todo_items = TodoListItem.objects.all()
        return render(request, 'todolist.html', {
            'all_items': all_todo_items
        })
  1. Add the deleting method
    • Add the html
    <ul>
        {% for i in all_items %}
        <li>
            {{i.name}}
            <form action="/deleteTodoItem/{{i.id}}/" method="post">{% csrf_token %}
                <input type="submit" value="Delete">
            </form>
        </li>
        {% endfor %}
    </ul>
    • Add the url
    path('deleteTodoItem/<int:i>/', deleteTodoView),
    • Add the view
    def deleteTodoView(request, i):
        y = TodoListItem.objects.get(id=i)
        y.delete()
        return HttpResponseRedirect('/')
  2. Resource: https://pythonistaplanet.com/to-do-list-app-using-django/

django-to-do-app's People

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.