def create(request): form = TodoForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/') else: return HttpResponseBadRequest()
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)
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 list(request, todolist_id, page=1): """ View created to list all the Todos, paginated by Five itens per page, and display in the last <li> a input for add new Todos. """ todolist = get_object_or_404(TodoList, pk=todolist_id) paginator = Paginator(todolist.todos.all(), 5) try: todos = paginator.page(int(page)) except EmptyPage: todos = [] data = { 'todolist': todolist, 'todo_form': TodoForm(), 'todos': todos, 'page': page, } return render_to_response('todos.html', data, context_instance=RequestContext(request))