Django url routing configuration composed from lists of mapping between url path
expressions and views functions. URL configuration could be more then just a
list. viewflow.fsm.Viewset
is the class that can be used as part of url
configuration. These allow you to structure your code and quickly connect and
customize predefined sets of URLs.
Viewflow provides viewset for organizing CRUD, Workflow logic, perform authentication and connect other 3d part packages. The default behavior could be customized by inheriting and overriding base classes attributes and methods.
from django.urls import path
from viewflow.urls import Viewset
class WebsiteViewset(Viewset):
index_path = path('', index_view, name='index')
Basically, a Viewset is the class with .urls property, suitable to include into Django root URL config in your main urls.py
viewset = WebsiteViewset()
urlpatterns = [
path('', viewset.urls)
]
viewset.urls list contains all paths from the class attributes with _path suffix
To mix url definition with other class method, an url attribute could be a property
@property
def page_path(self):
return path('page/', self.page_view, name='page'))
def page_view(self, request);
return render(request, 'page.html')
A Viewset could contain other viewset
from viewflow.urls import route, Viewset
class UserViewset(Viewset):
page_path = path('account/', self.account_view name="account")
class WebsiteViewset(Viewset):
users_path = route('auth/', UserViewset())
All Viewset URLs are prefixed with namespace. Each part of Django URL configuration could have application and instance namespace
By default, Viewset application namespace are determined from viewset class name, by lowering in and stripping common suffixes like Viewset. In the sample above, WebsiteViewset has ‘website:’ namespace, and nested UserViewset has ‘website:user:’
from django.urls import reverse
>>> reverse('website:index')
'/'
>>> reverse('website:page')
'/page/'
>>> reverse('website:user:account')
'/auth/account/'
To manually specify a viewset namespace, use app_name class attribute or pass the app_name keyword parameter to the viewset constructor.
class UserViewset(Viewset):
page_path = path('account/', self.account_view name="account")
class WebsiteViewset(Viewset):
app_name = 'main'
users_path = route('auth/', UserViewset(app_name='profile'))
>>> reverse('main:profile:account')
'/auth/account/'