【问题标题】:Overwrite django view with custom context (Django 1.11, Viewflow)使用自定义上下文覆盖 django 视图(Django 1.11,Viewflow)
【发布时间】:2017-11-01 22:37:00
【问题描述】:

我有一个使用 Viewflow 的 Django 1.11 项目 - https://github.com/viewflow/viewflow - 我已经合并了。这很有帮助,但很多东西有点“神奇”,作为我的第一个认真的 Django 项目,我遇到了一个我不知道如何解决或最好的方法的问题。

我有一个需要大量上下文的通用模板。我有一个函数可以将此上下文添加到我的所有视图中:

def add_general_context(context, MOC, MOC_enabled_fields = (), MOC_status = None):
  context['MOC'] = MOC
  context['current_date'] = timezone.now().strftime("%D")
  context['MOC_form'] = forms.MOCForm(prefix="MOC_form", MOC_enabled_fields=MOC_enabled_fields, instance=MOC)
  context['MOCAttachments'] = models.MOCAttachment.objects.filter(MOC=MOC)
  context['MOCAttachment_form'] = forms.MOCAttachmentForm(prefix="MOCAttachment_form")
  context['MOCApprovals'] = models.MOCApproval.objects.filter(MOC=MOC)
  context['MOCTasks'] = models.MOCTask.objects.filter(MOC=MOC)
  context['MOC_status'] = MOC_status
  context['MOCConversation'] = models.MOCConversation.objects.filter(MOC=MOC)
  # Add comments to the conversation
  for conversation in context['MOCConversation']:
    conversation.comments = models.MOCComment.objects.filter(conversation=conversation)
  context['MOCComment_form'] = forms.MOCCommentForm(MOC=MOC)
  context['MOCCommentReply_form'] = forms.MOCCommentReplyForm()

我基本上需要将此上下文添加到视图流内的视图中 - 即,AssignTaskView - https://github.com/viewflow/viewflow/blob/f50accb3cde5d53f1d4db0debf5936867712c3bd/viewflow/flow/views/task.py#L109

我尝试了一些方法来覆盖/添加到上下文中,但似乎都没有。

尝试 1:覆盖 URL 并使用 extra_context(建议这样做)
- 问题是 url 是“神奇的”,我的 urlpatterns 非常简单:

from material.frontend import urls as frontend_urls
urlpatterns = [
  url(r'^MOC/', include('MOC.urls')),
  url(r'', include(frontend_urls)),
]

覆盖 url 本身就是我的头,我研究了一段时间,但它使用非常通用的函数来拉入模块等。我不知道如何真正尝试它。

尝试 2:覆盖视图本身和 get_context_data 函数
我认为这是可能的,但它似乎不起作用。我的尝试看起来与此类似(最近一次):

from viewflow.flow.views.task import AssignTaskView as CoreAssignTaskView

class AssignTaskView(CoreAssignTaskView):
  def get_context_data(self, **kwargs):
    context = super(AssignTaskView, self).get_context_data(**kwargs)
    print("Did it work?")
    return context

这是在我的 views.py 中 - 但是,它根本不运行。我可能遗漏了一些东西,但我不知道如何实际强制它使用 my 视图而不是视图流中内置的视图。

我已经成功地覆盖了 Viewflow 的模板,没有任何问题,但是覆盖其他任何东西都超出了我的范围。有什么建议吗?

【问题讨论】:

    标签: python django django-1.11 django-viewflow


    【解决方案1】:

    是的,您实际上可以通过将视图 url 放在 url_patterns 之上来覆盖它

    urlpatterns = [
        url(
            r'^/workflow/appname/flowname/(?P<process_pk>\d+)/taskname/(?P<task_pk>\d+)/assign/$',
            YouCustomView.as_view(),
            {'flow_task': FlowClass.taskname},
            name="{}__assign".format(self.name)))
        ),
        url(r'', include(frontend_urls)),
    ]
    

    但是创建自定义flow.View子类并设置你自己的Assign View会更简单

    https://github.com/viewflow/viewflow/blob/master/viewflow/flow/nodes.py#L306

    from viewflow import flow
    
    class MyView(flow.View):
          assign_view_class = MyAssignTaskView
    

    flows.py:

     class MyFlow(Flow):
        ...
        taskname = MyView(UpdateProcessView).next(this.end)
    

    这就是您可以覆盖任何内置视图的方式。

    Viewflow 旨在提供代码库中的所有旋钮。您可以通过子类化 Flow 或 flow Node 类来自定义任何行为。

    自定义节点示例可能会有所帮助

    https://github.com/viewflow/viewflow/blob/master/demo/customnode/nodes.py

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 2018-02-01
      • 1970-01-01
      • 2020-10-27
      • 2020-04-18
      • 2018-02-01
      • 2017-10-04
      相关资源
      最近更新 更多