How to pass kwargs to Django viewflow flowurl template tag?

63 Views Asked by At

I am using the django-viewflow application with a custom view representing a stock item record where there are a number of workflows that can be attached to the record (registering/ issuing/ disposing etc.). In the detail view of the record I want to have a list of all workflows attached to the item and to be able to list all the tasks related to that workflow.

I am aiming for a url that looks like

  • .../asset/3/ - for the detail view
  • .../asset/3/register/93/ - for the register process (and subsequent tasks)
  • .../asset/3/movement/1/ - for movement process (and subsequent tasks)
  • etc

My application url.py looks like:

asset_urls = FlowViewSet(AssetFlow).urls
movement_urls = FlowViewSet(MovementFlow).urls

app_name = 'asset'
workflowurlpatterns = [
#    path('ajax/load-budget_lines/', views.load_budget_lines, name='ajax_load_budget_lines'),
    path('viewflow/', include((asset_urls, 'asset'))),
    path('movement/', include((movement_urls, 'movement'))),

#    path('', views.Detail.as_view(), name='detail'),
]


urlpatterns = [
    path('', views.List.as_view(), name='assets'),
    path('<int:asset_pk>/', views.Detail.as_view(), name='detail'),
    path('<int:asset_pk>/', include((workflowurlpatterns)))
]

The issue is that all the urls generated by the template tag {% flowurl task user=request.user as task_url %} fail with a NoReverseMatch error.

Is there a clever way to pass the asset_id so that the urls work (or another solution)?

An example of the error I get is

Reverse for 'finance_tc' with keyword arguments '{'process_pk': 93, 'task_pk': 411}' not found. 1 pattern(s) tried: ['asset/(?P<asset_pk>[0-9]+)/register/(?P<process_pk>\\d+)/finance_tc/(?P<task_pk>\\d+)/$']
2

There are 2 best solutions below

1
Md. Nazmul Hassan On

If you are using URL namespaces in Django project, make sure you specify the correct namespace when using the template tag. Namespaces are defined using the "app_name" attribute in the "urls.py" file and can be referenced in the template tag as "namespace:url_name".

1
AudioBubble On

try this code below

from django.urls import path, include
from viewflow.flow.viewset import FlowViewSet
from .flows import AssetFlow, MovementFlow
from . import views

asset_urls = FlowViewSet(AssetFlow).urls
movement_urls = FlowViewSet(MovementFlow).urls

app_name = 'asset'
workflowurlpatterns = [
    path('register/<int:workflow_pk>/', include(asset_urls)),
    path('movement/<int:workflow_pk>/', include(movement_urls)),
]

urlpatterns = [
    path('', views.List.as_view(), name='assets'),
    path('<int:asset_pk>/', views.Detail.as_view(), name='detail'),
    path('<int:asset_pk>/', include(workflowurlpatterns)),
]

Use this in django template

<a href="{{ task_url }}">{{ task.flow_class }}</a>