Package: django-fast-frontend
Django Fast FrontendOverviewDjango Fast Frontend is a Django app that provides an efficient way to customize frontend settings for your Django models. It provides aModelFrontendclass which allows you to specify various frontend configurations for your models.Note: Django Fast Frontend is a complementary package to DjangoÂ´s powerful MVT based frontend feature. If you require e.g. static pages for your website you can easily add them to your project using the original Django frontend feature.InstallationInstall Django Fast Frontend with pip:pipinstalldjango-fast-frontendThen addfrontendto yourINSTALLED_APPSin your Django project settings:INSTALLED_APPS=[# ...'django_bootstrap5','frontend',# ...]Quick StartAdd Django Fast Frontend urls to your Django projectsurls.py:fromdjango.urlsimportpathimportfrontendurlpatterns=[# ...path('',frontend.site.urls),# ...]Create a file calledfrontend.pyin one of your Django apps.Add a model to your frontend:importfrontendfromapp.modelsimport<your-model># Register your model here.@frontend.register(Author)classAuthorFrontend(frontend.ModelFrontend):passThis will create the following features by default:A responsive frontend websiteA navigation barA general landing page in your frontendA landing page for each Django app in your frontendA site with a table containing all fields and data of the Django model registered with Django Fast Frontend infrontend.pySetup authentication for sites generated by Django Fast Frontend:fromdjango.urlsimportpathimportfrontendurlpatterns=[# ...path('accounts/',frontend.accounts.urls),path('',frontend.site.urls),# ...]Django Fast Frontend: DocumentationConfigurationProject SettingsIn yoursettings.pyfile, you can set various global settings for Django Fast Frontend:# settings.py# ...FRONTEND_CUSTOM_CSS='css/custom.css'# Path to custom CSS fileFRONTEND_BRAND='Fast Frontend'# Brand nameFRONTEND_LOGO='img/django-fast-frontend-logo-text.PNG'# Logo file pathFRONTEND_DESCRIPTION="Powerful and interesting description for your frontend"# Description for your frontendFRONTEND_AUTHENTICATION=True# Whether authentication is required in general# ...URL ConfigurationIn yoururls.pyfile, include the Django Fast Frontend URLs:# urls.py# ...fromdjango.urlsimportinclude,pathimportfrontendurlpatterns=[# ...path('accounts/',include(frontend.accounts.urls)),path('',include(frontend.site.urls)),# ...]UsageDjango ModelsUseverbose_name_pluralandverbose_nameto display a correct name for your model tables in the frontendclass Author(models.Model):
    class Meta:
        verbose_name = "Author"
        verbose_name_plural = "Authors"
        db_table_comment = "A List of Authors"Creating a Model FrontendTo create a frontend for a model, you need to create a subclass offrontend.ModelFrontendand register it withfrontend.site. Here's an example for a model namedAuthor:# app/frontend.py# ...importfrontendfromapp.modelsimportAuthorclassAuthorFrontend(frontend.ModelFrontend):fields=('name','title')# Fields to displaylogin_required=False# Whether login is requiredlist_display=('name','title')# Fields to display in list viewinline_button=('inline_button1','inline_button2')# Methods for inline buttonsview_permission=False# Whether view permission is requiredcards=True# Whether to display instances as cardslist_filter=('name','title')# Fields for list view filtersearch_fields=('name','title','birth_date')# Fields for searchreadonly_fields=('name','title')# Fields that are read-onlychange_permission=True# Whether change permission is requireddelete_permission=True# Whether delete permission is requiredadd_permission=True# Whether add permission is requiredlist_per_page=5# Number of instances per pagetoolbar_button=('toolbar_button1','toolbar_button2')# Methods for toolbar buttonsdescription="Everything about authors."# Description for the frontendsortable_by=('name','title')# Fields that can be sorted# Define your custom methods here:deftoolbar_button1(self):# Your custom code here...deftoolbar_button2(self):# Your custom code here...definline_button1(self,object):# Your custom code here...definline_button2(self,object):# Your custom code here...# Register your model frontend:frontend.site.register(Author,AuthorFrontend)Customizing the FrontendYou can customize the appearance and behavior of your model frontend by overriding various attributes and methods in yourModelFrontendsubclass.Field DisplayTo customize which fields are displayed in the list view, setlist_displayto a tuple of field names:list_display=tuple()Field FilteringTo add a filter sidebar that lets users filter the list view by certain fields, setlist_filterto a tuple of field names:list_filter=tuple()Field SearchingTo enable a search box that lets users search the list view by certain fields, setsearch_fieldsto a tuple of field names:search_fields=tuple()Field SortingTo enable sorting of the list view by certain fields, setsortable_byto a tuple of field names:sortable_by=tuple()PaginationTo customize the number of instances displayed per page, setlist_per_pageto the desired number:list_per_page=100Read-Only FieldsTo make certain fields read-only in the frontend, setreadonly_fieldsto a tuple of field names:readonly_fields=tuple()Inline ButtonsYou can add custom inline buttons to your list view by defining methods for them in yourModelFrontendsubclass and adding them to theinline_buttonattribute:inline_button=('inline_button1','inline_button2')definline_button1(self,object):# Your custom code here...definline_button2(self,object):# Your custom code here...Note: The name of a buttons follows the name of the function.Toolbar ButtonsSimilarly, you can add custom toolbar buttons by defining methods for them and adding them to thetoolbar_buttonattribute:toolbar_button=('toolbar_button1','toolbar_button2')deftoolbar_button1(self):# Your custom code here...deftoolbar_button2(self):# Your custom code here...Note: The name of a buttons follows the name of the function.PermissionsYou can control permissions for viewing, adding, changing, and deleting instances by setting theview_permission,add_permission,change_permission, anddelete_permissionattributes:view_permission=Truechange_permission=Falsedelete_permission=Falseadd_permission=FalseNote that these permissions are checked in addition to the standard Django model permissions.DescriptionYou can add a description for your model frontend by setting thedescriptionattribute:description=str()This description will be displayed in the frontend.RegistrationFinally, to make your model frontend active, you need to register it withfrontend.site:frontend.site.register(Author,AuthorFrontend)This will create URLs for the list, add, change, and delete views for your model, based on the model's name.Custom CSSYou can provide a custom CSS file for your Django Fast Frontend by settingFRONTEND_CUSTOM_CSSin your Django project settings to the path of your CSS file:FRONTEND_CUSTOM_CSS='css/custom.css'This CSS file will be included in all Django Fast Frontend pages.BrandingYou can specify a custom brand name and logo for your Django Fast Frontend by settingFRONTEND_BRANDandFRONTEND_LOGOin your Django project settings:FRONTEND_BRAND='Fast Frontend'FRONTEND_LOGO='img/django-fast-frontend-logo-text.PNG'This brand name and logo will be displayed in the navbar of all Django Fast Frontend pages.You can provide a description for your Django Fast Frontend by settingFRONTEND_DESCRIPTIONin your Django project settings:FRONTEND_DESCRIPTION="This is a description for the Django Fast Frontend."This description will be displayed on the frontend's main page.Frontend URLBy default, Django Fast Frontend automatically generates URLs for your model frontends based on their names. If you want to override this, you can setFRONTEND_URLin your Django project settings to your desired URL path:FRONTEND_URL='/your-favorite-url-path/'Note that this URL path should start and end with a slash.AuthenticationBy default, Django Fast Frontend does not require users to be logged in to view your model frontends. If you want to require login, you can setFRONTEND_AUTHENTICATIONto True in your Django project settings:FRONTEND_AUTHENTICATION=TrueNote that this requires Django's authentication system to be properly configured.Important: To use Django Fast Frontend authentication for sites enable the frontend URLs:fromdjango.urlsimportpathimportfrontendurlpatterns=[# ...path('accounts/',frontend.accounts.urls),path('',frontend.site.urls),# ...]Note: It is possible to use a different authentication system.In addition, you can customize the URL users are redirected to after login and logout by settingLOGIN_REDIRECT_URLandLOGOUT_REDIRECT_URLin your Django project settings:LOGIN_REDIRECT_URL='/'LOGOUT_REDIRECT_URL='/'Integrating with Django's URL SystemTo integrate Django Fast Frontend with Django's URL system, you need to include its URLs in your Django project's URL configuration.For example, if you want to serve your Django Fast Frontend at the URL path/accounts/, you can add the following line to yoururls.pyfile:path('accounts/',include(frontend.accounts.urls)),Similarly, if you want to serve your Django Fast Frontend at the root URL path /, you can add the following line:path('',include(frontend.site.urls)),Customizing the Model FrontendDjango Fast Frontend provides a class-based system for customizing the frontend for each Django model. To create a frontend for a model, you need to create a subclass offrontend.ModelFrontendand register it withfrontend.site.Here is an example of how to create and register a frontend for theAuthormodel:importfrontendfromapp.modelsimportAuthorclassAuthorFrontend(frontend.ModelFrontend):# ...frontend.site.register(Author,AuthorFrontend)Customizing FieldsYou can specify which fields of the model to display in the frontend by setting thefieldsattribute:classAuthorFrontend(frontend.ModelFrontend):fields=('name','title')In this example, only thenameandtitlefields of theAuthormodel will be displayed.Customizing PermissionsYou can control whether users are allowed to view, add, change, or delete objects of the model by setting theview_permission,add_permission,change_permission, anddelete_permissionattributes, respectively:classAuthorFrontend(frontend.ModelFrontend):view_permission=Falsechange_permission=Truedelete_permission=Trueadd_permission=TrueIn this example, users are not allowed to view Author objects, but they are allowed to add, change, and delete them.Customizing DisplayYou can customize how objects of the model are displayed in the list view by setting thelist_displayattribute:classAuthorFrontend(frontend.ModelFrontend):list_display=('name','title')In this example, the list view will display thenameandtitlefields of eachAuthorobject.You can also specify which fields are available for sorting by setting thesortable_byattribute:classAuthorFrontend(frontend.ModelFrontend):sortable_by=('name','title')# List of fields available for sortingIn this example, users can sort the list view by thenameortitlefields.Customizing SearchYou can enable search for the model by setting thesearch_fieldsattribute:classAuthorFrontend(frontend.ModelFrontend):search_fields=('name','title','birth_date')In this example, users can searchAuthorobjects by theirname,title, orbirth_datefields.Customizing Inline ActionsYou can add inline actions to the model by defining methods on yourModelFrontendsubclass and including their names in theinline_buttonattribute:classAuthorFrontend(frontend.ModelFrontend):inline_button=('check','uncheck')defcheck(self,object):print(object.name)defuncheck(self,object):print(object.title)In this example, eachAuthorobject in the list view will have acheckanduncheckbutton. When clicked, these buttons will call the corresponding methods on theAuthorFrontendinstance, passing theAuthorobject as an argument.Customizing Toolbar ActionsSimilarly, you can add toolbar actions by defining methods and including their names in thetoolbar_buttonattribute:classAuthorFrontend(frontend.ModelFrontend):toolbar_button=('everything','everything_everything')defeverything(self):print(self)defeverything_everything(self):print(self)In this example, the frontend will have a toolbar with aneverythingandeverything_everythingbutton. When clicked, these buttons will call the corresponding methods on theAuthorFrontendinstance.Customizing PaginationYou can control how many objects are displayed per page in the list view by setting thelist_per_pageattribute:classAuthorFrontend(frontend.ModelFrontend):list_per_page=5In this example, the list view will display 5Authorobjects per page.Customizing FiltersYou can add a sidebar for filtering the list view by setting thelist_filterattribute:classAuthorFrontend(frontend.ModelFrontend):list_filter=('name','title')In this example, the list view will include a sidebar that allows users to filterAuthorobjects by theirnameortitlefields.Customizing Read-Only FieldsYou can specify which fields should be read-only by setting thereadonly_fieldsattribute:classAuthorFrontend(frontend.ModelFrontend):readonly_fields=('name','title')In this example, thenameandtitlefields will be displayed as read-only.Customizing CardsIf you want to display your data in a card format, you can set thecardsattribute toTrue:classAuthorFrontend(frontend.ModelFrontend):cards=TrueIn this example, theAuthorobjects will be displayed in a card format.Customizing Frontend DescriptionYou can add a description to your frontend by setting thedescriptionattribute:classAuthorFrontend(frontend.ModelFrontend):description=f"everything_everything everything_everything everything_everything "In this example, the description will be displayed on the frontend.Remember to register yourModelFrontendsubclass withfrontend.siteso that it is used:frontend.site.register(Author,AuthorFrontend)This completes the overview of Django Fast Frontend customization. It offers a high level of customization to create a frontend that meets your application's specific needs. Keep in mind that not all features are covered here, so make sure to explore the Django Fast Frontend's documentation for more advanced features and options.The ModelFrontend ClassTheModelFrontendclass is a major component of the Django frontend configuration. It inherits from theFrontendAbstractandNotImplementedMixinclasses and provides a large number of methods and properties that you can use to customize the frontend view for a model.Here are the primary components of theModelFrontendclass:Class VariablesThe class variables in theModelFrontendclass are used to configure the frontend views. They include options for login requirements, toolbar buttons, list display fields, pagination, permissions, search, sorting, filtering, readonly fields, and more. Each of these class variables can be overridden in subclasses to provide custom configurations for different models.InitializationTheModelFrontendclass is initialized with amodelkeyword argument, which specifies the Django model to configure frontend settings for. This model is stored in theself.modelinstance variable.def__init__(self,*args,**kwargs):self.model=kwargs.get('model',None)Get MethodsTheModelFrontendclass includes a number of get methods that return the values of the class variables. These methods can be overridden in subclasses to provide custom logic for determining the values of the class variables.defget_list_display(self):returnself.list_displayQueryset MethodThequerysetmethod gets objects of the model with the specified fields. It uses theget_fieldsmethod to determine which fields to include in the queryset. If 'id' is included in the fields, then the queryset values are limited to those fields. If no fields are specified, then all fields except 'id' are included in the queryset values.defqueryset(self,*args,**kwargs):...Pagination MethodTheget_paginationmethod gets a paginator for the objects and returns the paginated objects. It uses theget_list_per_pagemethod to determine how many objects to include on each page.defget_pagination(self,request,objects):...Model Actions MethodTheget_model_actionsmethod gets the actions to include in the table fields for the frontend view. Ifinline_buttonis defined, then these actions are added to the table fields.defget_model_actions(self,inline_button):...Search, Filter, and Sort MethodsTheget_search_results,get_filter_results, andget_sort_resultsmethods are used to apply search, filter, and sort parameters to the objects queryset. They use theget_search_fields,get_list_filter, andget_sortable_bymethods to determine which fields to use for searching, filtering, and sorting.defget_search_results(self,objects,search_fields,search_query):...defget_filter_results(self,objects,filter_fields,filter_args):...defget_sort_results(self,objects,sort_fields,sort_args):...Filter Options and Args MethodsTheget_filter_optionsandget_filter_argsmethods are used to get the filter options and arguments for the frontend view. They use theget_list_filtermethod to determine which fields to use for filtering.defget_filter_options(self):...defget_filter_args(self,request_get):...This class provides a comprehensive set of options and methods for customizing the frontend views for Django modelsOverriding ModelFrontend MethodsTheModelFrontendclass methods provide a powerful way to control the behavior of the frontend configuration for a Django model. While the default methods provide a good starting point, you can override these methods in your own subclasses to provide custom functionality.Here are some examples of how you can override these methods:Overriding the get_fields MethodYou might want to display different fields in the frontend view depending on some condition. For example, you might want to display certain fields only to authenticated users. You can accomplish this by overriding theget_fieldsmethod and adding a check for the user's authentication status.defget_fields(self):ifself.request.user.is_authenticated:return('field1','field2','field3')else:return('field1','field2')Overriding the get_search_results MethodYou might want to customize the way search queries are handled. For example, you might want to perform case-insensitive searches or use a different search algorithm. You can accomplish this by overriding theget_search_resultsmethod.defget_search_results(self,objects,search_fields,search_query):# Perform a case-insensitive searchquery=Q()forfieldinsearch_fields:query|=Q(**{f"{field}__icontains":search_query})objects=objects.filter(query)returnobjectsOverriding the get_pagination MethodYou might want to customize the number of objects displayed per page or the way pagination is handled. You can do this by overriding theget_paginationmethod.defget_pagination(self,request,objects):# Display 50 items per page instead of the default 100paginator=Paginator(objects,50)objects=paginator.get_page(request.GET.get("page"))returnobjectsBy overriding these and otherModelFrontendmethods, you can customize the frontend configuration for each Django model in your project.