You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# example/urls.py"""example URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.2/topics/http/urls/Examples:Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home')Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))"""fromdjango.contribimportadminfromdjango.urlsimportpath, includeurlpatterns= [
path('admin/', admin.site.urls),
path('', include('hello.urls')),
]
// hello/templates/hello/index.jsfunctiongetVersion(){varxhttp=newXMLHttpRequest();xhttp.onreadystatechange=function(){if(this.readyState==4&&this.status==200){document.getElementById('demoGet').innerHTML=this.responseText;}};xhttp.open('GET','version',true);xhttp.send();}functionpostAdd(){varxhttp=newXMLHttpRequest();xhttp.onreadystatechange=function(){if(this.readyState==4&&this.status==200){document.getElementById('demoPost').innerHTML=this.responseText;}};xhttp.open('POST','add',true);xhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xhttp.send("a=1&b=2");}// Would you like to know more?// https://www.w3schools.com/js/js_ajax_intro.asp
<!-- hello/templates/hello/index.html --><!DOCTYPE html><html><body><h1>Using XMLHttpRequest Object and Django JsonResponse</h1><h2>GET Example</h2><pid="demoGet">Let AJAX change this text.</p><buttontype="button" onclick="getVersion()">Run GET</button><h2>POST Example</h2><pid="demoPost">Let AJAX change this text.</p><buttontype="button" onclick="postAdd()">Run POST</button><script>{%include"hello/index.js"%}</script></body></html>