Writing Custom Views

Viewflow wraps Django views with workflow logic. It checks permissions, manages state, and injects request.activation. Your view code handles only the task itself.

When the task is done, call activation.execute() to mark it complete and create the next tasks.

A single view can work with different workflow nodes. Keep view code focused on the task, not the workflow.

Function-Based Views

def task_view(request, **kwargs):
    form = MyForm(request.POST or None)

    if form.is_valid():
        # Save form data
        object = form.save(commit=True)

        # Link to process
        request.activation.process.artifact = object

        # Complete the task
        request.activation.execute()

        # Redirect to next task
        return redirect(request.activation.get_success_url(request))

    return render(request, "task.html", {
        "form": form,
        "activation": request.activation
    })

Class-Based Views

Add self.request.activation.execute() in form_valid. Use TaskSuccessUrlMixin for redirect handling:

from viewflow.workflow.flow.views import mixins

class CustomFormView(mixins.TaskSuccessUrlMixin, generic.CreateView):
    model = SomeModel
    fields = ["acth", "estradiol", "free_t3", "free_t4"]

    def form_valid(self, form):
        object = form.save()
        object.save()

        # Link to process
        request.activation.process.artifact = object

        self.request.activation.execute()
        return redirect(self.get_success_url())