django-ajax-views Documentation Release 0.0.3b1 Emanuel Hafner Jan 04, 2017 Contents 1 Features 3 2 Contents 5 2.1 Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.2 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.3 ServerAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.4 ClientAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3 Indices 27 PythonModuleIndex 29 i ii django-ajax-viewsDocumentation,Release0.0.3b1 ThisappisanextensionofDjango’sclass-basedviewsandworkstogetherwithjavascriptlibraryrequire-ajax-views. It’smainpurposeistoencapsulateserver/clientcommunicationtoenableupdatingofviewswithasimplefunctioncall injavascript. ArgumentsparsedfromURLkwargsandthequerystringarehandledasonedatasetforeachrequest andreturnedassuchineachresponse. Thissimplifiesbuildingofcomplexviewswherefilterparametersarepartially passedthroughURLusingDjango’scleanURLdesignandasquerystringparameters. SomebasicknowledgeofDjango’sclass-basedgenericviewsandRequireJSwouldbedesirabletousethisapp. Contents 1 django-ajax-viewsDocumentation,Release0.0.3b1 2 Contents 1 CHAPTER Features • AjaxListViews – Ajaxabletemplatestoupdateviewsautomatically – Built-ingenericfiltersupport • GenericForms – Enhancedformactioncontrols – Displaypreviewtoconfirmactions • BootstrapModals – Supporttodisplayformanddetailviewsinmodals 3 django-ajax-viewsDocumentation,Release0.0.3b1 4 Chapter1. Features 2 CHAPTER Contents 2.1 Concept Theideaistocreateaninterfacebetweenserverandclientsideclassesthatknowhowtocommunicatewitheachother. ThisisdonebycreatingaJSfilewiththesamenameastheURLnamethatismappedtothecorrespondingDjango viewclass. ThatfileisloadedbyRequireJSandexecutedautomaticallyforallrequests. 2.1.1 Client Side Since javascript doesn’t support class definitions and inheritance I recommend using coffeescript or typescript to simply inherit from the base View() class. You can still use javascript though by using the built in extendjs functiontomimicclassinheritance. Listing2.1: my_view.js define(['ajaxviews'], function(ajaxviews) { var MyView = ajaxviews.extendjs(ajaxviews.View); MyView.prototype.onLoad = function () { // access class instance variables and methods with 'this' }; return MyView; }); Listing2.2: my_view.coffee define ['ajaxviews'], (ajaxviews) -> class MyView extends ajaxviews.View onLoad: -> # access class instance with '@' Listing2.3: my_view.ts define(['ajaxviews'], function(ajaxviews) { class MyView extends ajaxviews.View { onLoad() { // access class instance with 'this' } } } 5 django-ajax-viewsDocumentation,Release0.0.3b1 ForthistoworkyouneedtosetupRequireJSandplacetheJSfilesinsidetheviewsdirectorywhichislocatedinJS root. Inmain.jsrequiretheajaxviewsmoduleandinitializetheApp. ThiswillexecutetheView classwhose filenameequalstheURLnamefromDjango’sURLconf. Listing2.4: main.js // setup require config require(['ajaxviews'], function(ajaxviews) { var App = ajaxviews.App; App.config({ // options }); App.init(); }); 2.1.2 Server Side Theserversideajaxviewsappprovidesviewandformclassesyoucaninheritfrom. Listing2.5: urls.py from django.conf.urls import url from .views import MyAjaxView urlpatterns = [ url(r'^my/view/$', MyAjaxView.as_view(), name='my_view'), ] Listing2.6: views.py from django.views.generic import View from ajaxviews.views import GenericBaseView class MyAjaxView(GenericBaseView, View): ajax_view = True Theajaxviews.views.GenericBaseView takescareofpassingtheURLnametheviewclassismappedto, totheclientsideApp. Addajax_view = TruetotheclassifyouhavecreatedacorrespondingJSfile. Ifnotyou canomittheajax_viewpropertyorsetittoFalse. Listing2.7: base.html <script id="config" type="application/json">{{ json_cfg }}</script> The JSON config script is the communication channel for sites requested via URL. It’s included in the base html templatebytheAjaxMiddlewarealongwiththerequiremainscript. 6 Chapter2. Contents
Description: