From b6e8039367afb89675718ea27c67b50bd3062a2f Mon Sep 17 00:00:00 2001 From: shreys7 Date: Mon, 2 Dec 2019 06:14:12 +0530 Subject: [PATCH] Added urls.py and views.py inside todos app --- todoApp/todos/urls.py | 5 ++++- todoApp/todos/views.py | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/todoApp/todos/urls.py b/todoApp/todos/urls.py index a758cd64..4db747d8 100644 --- a/todoApp/todos/urls.py +++ b/todoApp/todos/urls.py @@ -1,6 +1,9 @@ from django.urls import path from . import views +app_name='todos' urlpatterns = [ - path('', views.IndexView.as_view(), name='Index') + path('', views.IndexView.as_view(), name='index'), + path('/edit', views.edit, name='edit'), + path('/save', views.save, name='save') ] \ No newline at end of file diff --git a/todoApp/todos/views.py b/todoApp/todos/views.py index 5ec9f07c..9497327f 100644 --- a/todoApp/todos/views.py +++ b/todoApp/todos/views.py @@ -1,6 +1,7 @@ -from django.shortcuts import render +from django.shortcuts import render, get_object_or_404, redirect from django.views import generic from .models import Todo +from django.http import HttpResponseRedirect class IndexView(generic.ListView): template_name = 'todos/index.html' @@ -10,3 +11,22 @@ def get_queryset(self): """Return all the latest todos.""" return Todo.objects.order_by('-created_at') +def save(request, todo_id): + + # form data from the request + title = request.POST.get('title') + description = request.POST.get('description') + + todo = get_object_or_404(Todo, pk=todo_id) + todo.title = title + todo.description = description + + todo.save() + + return redirect('todos:index') + +def edit(request, todo_id): + todo = get_object_or_404(Todo, pk=todo_id) + return render(request, 'todos/form.html', { + 'todo': todo + }) \ No newline at end of file