日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Django中用Jquery实现不刷新页面进行身份验证和计算器功能

發布時間:2024/4/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Django中用Jquery实现不刷新页面进行身份验证和计算器功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.下載jquery

http://www.jq22.com/jquery-info122

下載解壓之后加入工程中的static文件夾中

2.路由分發。

"""Django_demo1 URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views1. Add an import: from my_app import views2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views1. Add an import: from other_app.views import Home2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path from app01.views import classes, students , teachers from app01.views import ajax# from django.urls import re_pathurlpatterns = [path('admin/', admin.site.urls),path('classes.html/', classes.get_classes),path('add_classes', classes.add_classes),path('del_classes', classes.del_classes),path('edit_classes', classes.edit_classes),path('students.html/', students.get_students),path('add_students', students.add_students),path('del_students', students.del_students),path('edit_students', students.edit_students),path('teachers.html/', teachers.get_teachers),path('cls_add_teachers', classes.cls_add_teachers),path('ajax1.html', ajax.ajax1),path('ajax2.html', ajax.ajax2),path('ajax3.html', ajax.ajax3), ]

 

3.views文件夾中創建ajax視圖函數ajax.py

from django.shortcuts import render, redirect, HttpResponsedef ajax1(request):return render(request, 'ajax1.html')def ajax2(request):user = request.GET.get('username')pwd = request.GET.get('password')import timetime.sleep(5)return HttpResponse('ok')# return render(request, 'ajax2.html')def ajax3(request):try:v1 = request.POST.get('v1')v2 = request.POST.get('v2')print(v1, v2)answer = int(v1) + int(v2)except Exception as e:answer = '輸入格式錯誤!'return HttpResponse(answer)

4.在模板中添加ajax1.html文件

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title><style>.btn{display: inline-block;padding: 5px 15px;background-color: darkgoldenrod;color: white;cursor:pointer;}</style> </head> <body><div><input placeholder="用戶名" type="text" id="username"><input placeholder="密碼" type="password" id="password"><div class="btn" οnclick="submitForm()">提交</div></div><div><input placeholder="v1" type="text" id="v1" name="v1"><input placeholder="v2" type="text" id="v2" name="v2"><div class="btn" οnclick="add_function()">相加等于</div><input placeholder="answer" type="text" id="answer"></div><script src="/static/jquery-3.3.1.js"></script><script>function submitForm(){var u = $('#username').val()var p = $('#password').val()console.log(u,p)$.ajax({url:'/ajax2.html',type:'GET',data:{username:u, password: p},success:function (arg){console.log(arg)}})}function add_function(){var add1 = $('#v1').val()var add2 = $('#v2').val()console.log(add1,add2)$.ajax({url:'/ajax3.html',type:'POST',data:{'v1': add1, 'v2': add2},success:function (arg){console.log(arg)$('#answer').val(arg)}})}</script> </body> </html>

?

轉載于:https://www.cnblogs.com/CK85/p/10199074.html

總結

以上是生活随笔為你收集整理的Django中用Jquery实现不刷新页面进行身份验证和计算器功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。