Django中用Jquery实现不刷新页面进行身份验证和计算器功能
1.下载jquery
http://www.jq22.com/jquery-info122
下载解压之后加入工程中的static文件夹中

2.路由分发。
"""Django_demo1 URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/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'))
"""
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_path urlpatterns = [
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, HttpResponse def ajax1(request):
return render(request, 'ajax1.html') def ajax2(request):
user = request.GET.get('username')
pwd = request.GET.get('password')
import time
time.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" onclick="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" onclick="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>
Django中用Jquery实现不刷新页面进行身份验证和计算器功能的更多相关文章
- jQuery实现局部刷新页面数据绑定
今天遇到了一个问题:怎么样才能做到只刷新页面中的Repeater控件中的数据,在不用UploadPannel的情况下? 试了好多方法,无意间在看jquery文件时发现,使用load()方法即可解决此问 ...
- ASP.Net中无刷新执行Session身份验证
在写一个客户的B/S结构应用程序时,突然发现一个技巧,不知道是否是MS的一个BUG,给相关的有研究的朋友原先考虑写一个检查Session的类,Session失效后,必须转向登陆页面,可每一个调用该类的 ...
- Javascript刷新页面的八种方法
/** * Javascript刷新页面的八种方法 * 说明一下,jQuery没有发现刷新页面的方法. */ 1 history.go(0) 2 location.reload() 3 locatio ...
- js 如何刷新页面
Javascript刷新页面的几种方法(未测试):1 history.go(0)2 location.reload()3 location=location4 location.assign(loca ...
- django使用email进行身份验证(转载)
版权所有,转载请注明出处:http://guangboo.org/2013/03/27/authentication-using-email-in-django django自带的验证功能免去了我们的 ...
- jquery mobile页面跳转后,必须重新刷新页面js方可有效
最近在做个项目,用到jquery mobile,很陌生对他,问题一个个的来,那就要一个个解决,找了一天这个问题,放可明白:首先明白jqm里面页面跳转默认都是通过ajax请求的,必须重新刷新页面js方可 ...
- AJAX JQuery 调用后台方法返回值(不刷新页面)
AJAX JQuery 调用后台方法返回值(不刷新页面) (1)无参数返回值(本人亲试返回结果不是预期结果) javascript方法: $(function () { //无 ...
- jQuery实现发送验证码30s倒计时,且刷新页面时有效
在这里讲一讲这个案例的实现思路吧(个人见解)..核心思想:为防止页面刷新时倒计时失效的解决方案是:当每次刷新一次页面时都执行一个函数 即下面讲到的 setStyle() 函数.这个函数会根据当前的 c ...
- 使用jquery结合ajax做下拉刷新页面,上拉加载页面,俗称分页
jquery结合iscroll.js做下拉刷新页面,上拉加载页面 先上代码,里面都有注释这就不一一说明了 <!DOCTYPE html> <html lang="en&qu ...
随机推荐
- SpringMvc redirect
SpringMVC redirect 核心 首先网上百度到的资源基本已经够用, 留作记录. SpringMVC-redirect重定向跳转传值 虽然这哥们也是转的, 但又没有留源地址. 因此 ... ...
- 网络请求及各类错误代码含义总结(包含AFN错误码大全)
碰见一个很奇葩的问题, 某些手机在设置了不知什么后, 某些 APP 死活 HTTPS 请求失败, 例如以 UMeng 统计HTTP 请求失败为例, Log如下: UMLOG: (Error App ...
- 基于Cython和内置distutils库,实现python源码加密(非混淆模式)
起因 python本身只能做混淆,不能加密,多年的商业软件开发经验导致有某种"洁癖"欲望,将py编译打包 尝试 pyinstaller原理是freeze打包pyc文件,利用工具可完 ...
- 用AndroidSDK中的Face Detector实现人脸识别
很多手机图片管理应用都开始集成人脸识别功能.一提到人脸识别,模式识别,滤波,BlahBlah 一堆复杂的技术名字戳入脑海中,立刻觉得这玩意儿没法碰,太玄乎了.其实Android SDK从1.0版本中( ...
- Mac 10.12安装窗口快速缩放到一定比例的工具Moom
说明:比如想要窗口只占用屏幕的一半时,我们的做法就是手动调节,而这款软件已经有快速按钮点一下即可. 下载: (链接: https://pan.baidu.com/s/1gfguJth 密码: wqjb ...
- Mac 10.12安装粘贴板增加工具ClipMenu
说明:这个工具可以保留复制过的记录,并且可以快速调出之前复制过的内容,最开发时比较常用,支持图片等. 下载: (链接: https://pan.baidu.com/s/1qXJbM2o 密码: wef ...
- 成倍提高服务器的负载能力:浅谈Jexus的ASP.NET前置缓存技术
一.什么是“ASP.NET前置缓存” ASP.NET前置缓存,是Jexus特色功能之一,是指Jexus把开发者指定的ASP.NET网页某一时刻的内容,缓存到专用的高速缓冲区中,在指定的时间内, ...
- 连接Git@OSC操作步骤
一.准备工作 软件下载 Git:地址 TortoiseGit:地址 二.安装与配置 1.Git安装 Git配置 设置客户端的用户名和email 然后,到Git@OSC 上面注册一个帐号. 这个帐号就是 ...
- java io 处理流,BufferdInputStream、BufferedWriter、BufferedReader
BufferdInputStream:读取helloworld.java helloworld.java: package io; public class HelloWorld { public s ...
- html转图片,java库cssbox
引入依赖包 <dependency> <groupId>net.sf.cssbox</groupId> <artifactId>cssbox</a ...