URLPatterns autogeneration mechanisms: Router¶
One of the key architectural concepts of CRUDLFA+ is the ability to tie a group of view with a model class to autogenerate urlpatterns. This chapter reviews the different mechanisms in place and how they are overridable.
Source is located in the Router
, which
we’ll describe here.
The CRUDLFA+ Router is able to generate menus checking perms, generate urls …
Note
Note that you can also use non-database backed models, by inheriting from models.Model and setting their Meta.managed attribute to False. Then, you can use CRUDLFA+ views and routers.
Menus¶
A menu is referenced by a short name, and CRUDLFA+ generic views already define a bunch of them, but you can add your own too:
object
: means the view is for a model instance,object_detail
: means the view should only be visible from detail view,model
: means the view applies to a model class, such as list view,main
: means the view should be in the main menu.
To get the views of a router, for a menu, kwargs such as the object, and with
permissions on request.user use Router.get_menu()
. In Jinja2
templates you can call them with:
{% set views=view.router.get_menu(
'object',
view.request,
object=view.object
) %}
Now that Django can generate a menu after serious the refactoring that brought us to discover this pattern with Etienne Vidal @ DevNix, we rely on Jinja2 to refactor the HTML to render those menus.
The menu macro takes a list of views as argument, and also a unique HTML id it can use to generate the dropdown.
{% import 'crudlfap.html' as crudlfap %}
{{ crudlfap.dropdown(views, 'row-actions-' + str(object.pk)) }}
{# also works, different style: #}
{{ crudlfap.dropdownbutton(views, 'row-actions-' + str(object.pk)) }}
The above code will generate a Material design dropdown menu with an icon and the other one as a button with all nice icons, titles, permissions checked, and so on. This is used everywhere you see a part of the page that can spawn to a dropdown. If there is only one matching view, it will display only the button.
-
class
crudlfap.router.
Router
(model=None, registry=None, views=None, **attributes)[source]¶ Base router for CRUDLFA+ Route.
-
model
¶ Optional model class for this Router and all its views.
-
views
¶ ViewsDescriptor
usingCRUDLFAP_VIEWS
by default, otherwise your list of views.Note
The final views list is generated by the
generate_views()
method.
-
generate_views
(*views)[source]¶ Generate views for this router, core of the automation in CRUDLFA+.
This method considers each view in given args or self.views and returns a list of usable views.
Each arg may be a view class or a dict of attributes with a _cls key for the actual view class.
It will copy the view class and bind the router on it in the list this returns.
For example, this would cause two view classes to be returned, if self.model is
Artist
, thenCreateView
will be used as parent to createArtistCreateView
andDetailView
will be used to createArtistDetailView
, also setting the attributeextra_stuff='bar'
:Router(Artist).generate_views([ CreateView, dict(_cls=DetailView, extra_stuff='bar'), ListView.factory(paginate_by=12), ])
-
get_menu
(name, request, **kwargs)[source]¶ Return allowed view objects which have
name
in theirmenus
.For each view class in self.views which have
name
in theirmenus
attribute, instanciate the view class withrequest
and kwargs, callhas_perm()
on it.Return the list of view instances for which
has_perm()
has passed.
-
get_urlfield
()[source]¶ Return Field name of model for reversing url.
This will return model ` slug ` field if available or ` pk ` field.
See
guess_urlfield()
for detail.
-
register
()[source]¶ Register to self.registry.
Also, adds the get_absolute_url() method to the model class if it has None, to return the reversed url for this instance to the view of this Router with the
detail
slug.Set get_absolute_url in your model class to disable this feature. Until then, you got it for free.
Also, register this router as default router for its model class in the RouterRegistry.
-