def item_create(request, todolist_id, **kwargs): if 'todolist' in kwargs.keys(): curr_list = kwargs['todolist'] else: curr_list = get_object_or_404(TodoList, pk=todolist_id) note = TodoItem() title = request.POST['title'] done = True if 'done' in request.POST.keys() else False try: order = int(request.POST['order']) except ValueError: order = None note.todolist = curr_list note.title = title note.order = order note.done = done note.save() return HttpResponseRedirect(reverse('todo:item_index', args=(todolist_id,)))
def edit(request, id=None): if id: action = 'edit' todo = get_object_or_404(TodoItem, pk=id) else: action = 'new' todo = TodoItem() if request.POST: form = TodoItemForm(request.POST, initial=todo) if form.is_valid(): todo.name = form.cleaned_data.get('name') todo.done = form.cleaned_data.get('done') todo.save() redirect_url = reverse(index) return HttpResponseRedirect(redirect_url) else: form = TodoItemForm(initial={'name': todo.name, 'done': todo.done }) context = { 'form': form, 'action': action, } return TemplateResponse(request, 'edit.html', context=context)