def add_task(request): if request.method == 'POST': form = TaskForm(request.POST) if form.is_valid(): form.save(commit=True) return index(request) else: print form.errors else: form = TaskForm() return render(request, 'app/add_task.html', {'form': form})
def edit(request, pk): task = Task.objects.get(id=pk) form = TaskForm(instance=task) if request.method == 'POST': form = TaskForm(request.POST, instance=task) if form.is_valid(): form.save() return redirect('index') context = { 'tasks': Task.objects.all(), 'form': form, } return render(request, 'edit_task.html', context)
def index(request): if request.method == 'GET': context = { 'form': TaskForm, 'tasks': Task.objects.all(), } return render(request, 'index.html', context) else: form = TaskForm(request.POST) if form.is_valid(): form.save() return redirect('index') context = { 'tasks': Task.objects.all(), 'form': form, } return render(request, 'index.html', context)