An introduction into the forays of django
- Create a new project
django-admin startproject todo
- Start the server
cd todo && python3 manage.py runserver
- Create a new application within the project
python3 manage.py startapp todoapp
- Register the new application
installed_apps = [
...,
todoapp,
...,
]
- Create the template
mkdir templates
cd templates && touch todolist.html
- 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>
- 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
})
- 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),
]
- 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',
],
},
},
]
- 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),
]
- 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()
- makemigrations
python3 manage.py makemigrations
- migrate
python3 manage.py migrate
- 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
- register the model in admin
from django.contrib import admin
# Register your models here.
from .models import TodoListItem
admin.site.register(TodoListItem)
- add the str method
class TodoListItem(models.Model):
name = models.TextField()
def __str__(self):
return self.name
-
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('/')
- Add this into todolist.html
-
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
})
- 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('/')
- Resource: https://pythonistaplanet.com/to-do-list-app-using-django/