def create(request): form = TodoForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/') else: return HttpResponseBadRequest()
def home(request): form = TodoForm() todos = Todo.objects.all() if request.method == 'POST': form = TodoForm(request.POST) if form.is_valid(): form.save() return redirect('home') return render(request, 'core/home.html', {'form': form, 'todos': todos})
def update(request, todo_id): todo = Todo.objects.get(id=todo_id) form = TodoForm(instance=todo) if request.method == 'POST': form = TodoForm(request.POST, instance=todo) if form.is_valid(): form.save() return redirect('home') return render(request, 'core/update.html', {'form': form, 'todo': todo})
def add(request, todolist_id): """ View created to add a new Todo, Receiving an POST request and send it to the TodoForm, for beeing saved, if the form is valid. """ todolist = get_object_or_404(TodoList, pk=todolist_id) todo_form = TodoForm(request.POST or None) if todo_form.is_valid(): todo = todo_form.save(todolist=todolist) messages.success(request, u'Todo "%s" created with success.' % todo) else: messages.error(request, u'The form was not filled correctly.') return redirect('todos', todolist_id)