【问题标题】:How to set redirect route for django viewflow end?如何为 django viewflow 端设置重定向路由?
【发布时间】:2020-12-08 09:25:44
【问题描述】:

如何指定当流程以flow.End() 结束时重定向用户的自定义 url?

我尝试将覆盖 get_success_url() 的自定义视图传递给 End 节点,但它不起作用。

flows.py


end = flow.End(
    cancel_view=views.WeightTicketEndCancelView,
    detail_view=views.WeightTicketEndDetailView,
    undo_view=views.WeightTicketEndUndoView,
    perform_view=views.WeightTicketEndPerformView,
    view_or_class=views.WeightTicketEndView,
)

views.py

class WeightTicketEndPerformView(PerformTaskView):

    def get_success_url(self):
        return "/test/"

【问题讨论】:

    标签: python django django-viewflow


    【解决方案1】:

    对视图流很感兴趣,但要重定向,您需要导入 redirect

    from django.shortcuts import render, redirect
    

    然后,要重定向,请执行以下操作:

    return redirect('your_url_or_url_name')
    

    【讨论】:

    • 感谢您的回答。我知道在 django 中,用户使用django.shortcuts.redirect 重定向。问题是,我不知道在django-viewflow 中设置它的位置。
    【解决方案2】:

    结束节点刚刚完成流程。用户从未被重定向到端节点。

    你需要更改最新用户任务视图的`get_success_url

    【讨论】:

      【解决方案3】:

      感谢kmmbvnr 的解释,我需要更新get_success_url

      问题是,在执行最新的用户任务后获取所需的 url。 具体来说,我想将用户从一个流程重定向到另一个流程的第一个任务。

      我解决了将next_url 存储在process 模型中的问题:

      
      
      # flows.py
      class FirstFlow(Flow):
          process_class = FirstFlowProcess
      
          start = flow.Start(CreateProcessView).Next(this.start_second_flow)
      
          start_second_flow = (
              flow.View(CustomProcessView, fields=["start_second_flow"])
              .Next(this.check_start_second_flow)
          )
      
          check_start_second_flow = (
              flow.If(lambda activation: activation.process.start_second_flow)
              .Then(this.create_second_flow)
              .Else(this.end)
          )
      
          create_second_flow = flow.Handler(this.init_second_flow).Next(this.end)
      
          end = flow.End()
      
          def init_second_flow(self, activation):
              second_flow_activation = SecondFlow.start.run()
              activation.process.next_url = get_first_task_url(activation=second_flow_activation)
              activation.process.save()
      
      
      class SecondFlow(Flow):
      
          start = flow.StartFunction(this.start_second_process).Next(this.next_task)
      
          next_task = (flow...)
      
          @method_decorator(flow.flow_start_func)
          def start_second_process(self, activation):
              activation.prepare()
              activation.done()
              return activation
      
      def get_first_task_url(activation):
          first_task = activation.process.active_tasks().first()
          return first_task.flow_task.get_task_url(
              first_task,
              url_type='guess',
              user=User.objects.filter(is_superuser=True).order_by("?")[0],
              namespace="app_name:flow",
          )
      
      
      # models.py
      class FirstFlowProcess(Process):
          start_second_flow = models.BooleanField(default=False)
          next_url = models.CharField(max_length=1000, blank=True, null=True)
      
      
      # views.py
      class CustomProcessView(FlowMixin, generic.CreateView):
          template_name = "template_name.html"
          form_class = forms.FormClass
          
          def get_success_url(self):
              self.activation.process.refresh_from_db()  # update changes from the database
              if self.activation.process.next_url:
                  return self.activation.process.next_url
              else:
                  return super(CustomProcessView, self).get_success_url()
      
          def form_valid(self, form):
              self.activation_done()
              return redirect(self.get_success_url())
      
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-02
        • 2014-10-22
        • 1970-01-01
        相关资源
        最近更新 更多