Skip to content

Commit

Permalink
Added urls.py and views.py inside todos app
Browse files Browse the repository at this point in the history
  • Loading branch information
shreys7 committed Dec 2, 2019
1 parent e532e1d commit b6e8039
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 4 additions & 1 deletion todoApp/todos/urls.py
Original file line number Diff line number Diff line change
@@ -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('<int:todo_id>/edit', views.edit, name='edit'),
path('<int:todo_id>/save', views.save, name='save')
]
22 changes: 21 additions & 1 deletion todoApp/todos/views.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
})

0 comments on commit b6e8039

Please sign in to comment.