def update_todo(request, todo_id): todo = get_object_or_404(Todo, id=todo_id) form = TodoForm(request.POST or None, request.FILES or None, instance=todo) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('home:todo_list')) return render(request, "form.html", {"form": form})
def test_empty_description(self): form = TodoForm({'description': ''}) self.assertFalse(form.is_valid()) self.assertEqual( form.errors, {'description': [u'This field is required.']} )
def add_todo(request): form = TodoForm(request.POST or None, request.FILES or None) context = {"form": form} if form.is_valid(): form.save() return HttpResponseRedirect(reverse('home:todo_list')) return render(request, "form.html", context)
def test_too_title(self): form = TodoForm(self.too_long_description) self.assertFalse(form.is_valid()) self.assertEqual( form.errors, {'description': [u'Ensure this value has at most 128 ' + 'characters (it has 129).']} )
def add_todo(request, todolist_id): if request.method == 'POST': form = TodoForm(request.POST) if form.is_valid(): user = request.user if request.user.is_authenticated else None todo = Todo( description=request.POST['description'], todolist_id=todolist_id, creator=user ) todo.save() return redirect('home:todolist', todolist_id=todolist_id) else: return render(request, 'home/todolist.html', {'form': form}) return redirect('home:index')
def todolist(request, todolist_id): todolist = get_object_or_404(TodoList, pk=todolist_id) if request.method == 'POST': redirect('home:add_todo', todolist_id=todolist_id) return render( request, 'home/todolist.html', {'todolist': todolist, 'form': TodoForm()} )
def test_valid_input(self): form = TodoForm(self.valid_form_data) self.assertTrue(form.is_valid())
def index(request): return render(request, 'home/index.html', {'form': TodoForm()})