Skip to content

Commit dc5b9ed

Browse files
Update Lab.md
1 parent 1fdb303 commit dc5b9ed

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

Lab.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,126 @@ def error_view(request):
220220

221221

222222

223+
224+
# Lab 3 Ajax
225+
226+
```python
227+
228+
pip install virtualenv
229+
230+
python -m venv myenv
231+
232+
myenv\Scripts\activate
233+
234+
235+
pip install django
236+
237+
```
238+
239+
# Setups
240+
241+
```python
242+
django-admin startproject project1
243+
244+
cd project1
245+
```
246+
247+
# then next create ur view.py file and add this code i mentioned here
248+
249+
```python
250+
251+
from django.shortcuts import render
252+
from django.http import JsonResponse
253+
def home(request):
254+
return render(request, 'index.html')
255+
256+
def python_funct(request):
257+
#do something with the data passed
258+
259+
fname=request.GET.get('fname',None)
260+
lname=request.GET.get('lname',None)
261+
fullname= fname+' '+lname
262+
#fullname= 33
263+
response = {
264+
'fullname': fullname
265+
}
266+
return JsonResponse(response)
267+
```
268+
# create a folder templates/html file. Add this code here below
269+
```python
270+
271+
<html>
272+
<head>
273+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
274+
<script>
275+
function fullName() {
276+
//var fname= document.getElementById("fname").value;
277+
//var lname= document.getElementById("lname").value;
278+
//var fullname={%url "python_funct"%}
279+
280+
//ajax call to python function
281+
//$("#datadiv").find("select, textarea, input").serialize()
282+
$.ajax({data:$("#datadiv").find("input").serialize(),
283+
url: "{% url 'python_funct' %}",
284+
type: "GET",
285+
success: successFunc,
286+
error: errorFunc
287+
});
288+
289+
function successFunc(response) {
290+
//alert('ok');
291+
document.getElementById("p1").innerHTML = response.fullname;
292+
}
293+
function errorFunc() {
294+
alert('error');
295+
}
296+
297+
//fullname=2;
298+
//var fullname=2;
299+
300+
}
301+
</script>
302+
</head>
303+
304+
<body>
305+
306+
<div align=center id="datadiv">
307+
<p>Hello!</p>
308+
First Name :<input type=text name=fname id=fname><br>
309+
Last Name :<input type=text name=lname id=lname><br>
310+
<button onclick="fullName()">What's full name?</button>
311+
<p id="p1">t</p>
312+
<div>
313+
314+
</body>
315+
</html>
316+
```
317+
318+
# After that url creation look into urls.py file and add this file
319+
320+
```python
321+
322+
"""project1 URL Configuration
323+
324+
The `urlpatterns` list routes URLs to views. For more information please see:
325+
https://docs.djangoproject.com/en/3.2/topics/http/urls/
326+
Examples:
327+
Function views
328+
1. Add an import: from my_app import views
329+
2. Add a URL to urlpatterns: path('', views.home, name='home')
330+
Class-based views
331+
1. Add an import: from other_app.views import Home
332+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
333+
Including another URLconf
334+
1. Import the include() function: from django.urls import include, path
335+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
336+
"""
337+
from django.contrib import admin
338+
from django.urls import path
339+
from project1 import views
340+
urlpatterns = [
341+
path('admin/', admin.site.urls),
342+
path('', views.home, name='home'),
343+
path('pfunct', views.python_funct, name='python_funct'),
344+
]
345+
```

0 commit comments

Comments
 (0)