viewflow.io:我如何呈现用于收集特定任务输入的自定义表单?

viewflow.io: How can I present a custom form for gathering input for a specific task?

我想使用 ViewFlow(请参阅 http://viewflow.io/)进行用户注册过程,我要求输入用户密码(和密码 2 以进行确认)。我不想存储纯密码,但我想立即创建用户并分配密码,而不是将该数据存储在进程中。

您不必将所有原始数据存储在流程模型中。流程模型是所有流程相关数据的根条目。只需将 ForeignKey 添加到自定义流程模型中的用户

from django.contrib.auth.models import User
from viewflow.models import Process

class NewUserProcess(Process):
    user = models.ForeignKey(User, blank=True, null=True)

对于表单处理,viewflow 没有向标准 django 添加任何新内容。像往常一样,您可以使用基于 django 标准 class 的视图或基于普通功能的视图,以及您需要的任何表单处理代码。以下是基于功能的视图的示例:

from viewflow import flow
from viewflow.base import Flow
from viewflow.flow import flow_start_view


class NewUserFlow(Flow):
     registration = flow.StartView(registraton_view).Next(...)


@flow_start_view
def registraton_view(request, activation):
     activation.prepare(request.POST or None, user=request.user)
     form = RegistrationForm(request.POST or None)

     if form.is_valid():
         activation.process.user = form.save()
         activation.done()
         return redirect(...)

     return render(request, 'registration.html', {
        'form': form,
        'activation': activation,
     })

对于基于 class 的视图,您可以像往常一样指定自定义表单 class:

from viewflow.flow.views import StartFlowMixin

class RegistrationView(StartFlowMixin, generic.CreateView):
    form_class = RegistrationForm

    def activation_done(self, form):
        self.activation.process.user = self.object
        self.activation.done()

此外,您可以查看viewflow自定义视图演示源代码 - https://github.com/viewflow/cookbook/blob/master/custom_views/demo/bloodtest/views.py