File tree 3 files changed +29
-0
lines changed
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>
15
15
</ ul >
16
16
< p > < a href ="/admin/ "> Django Admin</ a > </ p >
17
17
{% 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 >
18
34
</ body >
19
35
</ html >
Original file line number Diff line number Diff line change 2
2
from django .urls import include , path
3
3
from django .views .generic import TemplateView
4
4
5
+ from example .views import increment
6
+
5
7
urlpatterns = [
6
8
path ("" , TemplateView .as_view (template_name = "index.html" )),
7
9
path ("jquery/" , TemplateView .as_view (template_name = "jquery/index.html" )),
8
10
path ("mootools/" , TemplateView .as_view (template_name = "mootools/index.html" )),
9
11
path ("prototype/" , TemplateView .as_view (template_name = "prototype/index.html" )),
10
12
path ("admin/" , admin .site .urls ),
13
+ path ("ajax/increment" , increment , name = "ajax_increment" ),
11
14
path ("__debug__/" , include ("debug_toolbar.urls" )),
12
15
]
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