File tree Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -15,5 +15,21 @@ <h1>Index of Tests</h1>
1515 </ ul >
1616 < p > < a href ="/admin/ "> Django Admin</ a > </ p >
1717 {% endcache %}
18+ < p >
19+ < span > Value </ span >
20+ < span id ="session-value "> {{ request.session.value|default:0 }}</ span >
21+ < button id ="increment " data-url ="{% url 'ajax_increment' %} " type ="button "> Increment</ button >
22+ </ p >
23+ < script >
24+ const increment = document . querySelector ( "#increment" ) ;
25+ const value = document . querySelector ( "#session-value" ) ;
26+ increment . addEventListener ( "click" , function ( ) {
27+ fetch ( increment . dataset . url ) . then ( function ( response ) {
28+ response . json ( ) . then ( function ( data ) {
29+ value . innerHTML = data . value ;
30+ } ) ;
31+ } ) ;
32+ } ) ;
33+ </ script >
1834 </ body >
1935</ html >
Original file line number Diff line number Diff line change 22from django .urls import include , path
33from django .views .generic import TemplateView
44
5+ from example .views import increment
6+
57urlpatterns = [
68 path ("" , TemplateView .as_view (template_name = "index.html" )),
79 path ("jquery/" , TemplateView .as_view (template_name = "jquery/index.html" )),
810 path ("mootools/" , TemplateView .as_view (template_name = "mootools/index.html" )),
911 path ("prototype/" , TemplateView .as_view (template_name = "prototype/index.html" )),
1012 path ("admin/" , admin .site .urls ),
13+ path ("ajax/increment" , increment , name = "ajax_increment" ),
1114 path ("__debug__/" , include ("debug_toolbar.urls" )),
1215]
Original file line number Diff line number Diff line change 1+ from django .http import JsonResponse
2+
3+
4+ def increment (request ):
5+ try :
6+ value = int (request .session .get ("value" , 0 )) + 1
7+ except ValueError :
8+ value = 1
9+ request .session ["value" ] = value
10+ return JsonResponse ({"value" : value })
You can’t perform that action at this time.
0 commit comments