当前位置: 首页 > news >正文

网站建设及网站推广网站开发与建设课程

网站建设及网站推广,网站开发与建设课程,wordpress主题 qux,简单网页制作视频教程【Django】教程-1-安装创建项目目录结构介绍 【Django】教程-2-前端-目录结构介绍 【Django】教程-3-数据库相关介绍 【Django】教程-4-一个增删改查的Demo 【Django】教程-5-ModelForm增删改查规则校验【正则钩子函数】 【Django】教程-6-搜索框-条件查询前后端 【Django】教程…

【Django】教程-1-安装+创建项目+目录结构介绍
【Django】教程-2-前端-目录结构介绍
【Django】教程-3-数据库相关介绍
【Django】教程-4-一个增删改查的Demo
【Django】教程-5-ModelForm增删改查+规则校验【正则+钩子函数】
【Django】教程-6-搜索框-条件查询前后端
【Django】教程-7-分页,默认使用django的
【Django】教程-8-页面时间组件
【Django】教程-9-登录+退出

16. ajax

16.1 GET请求

login目录text.html

{% extends 'login/layout.html' %}
{% block content %}<h3>示例 1</h3><input id="btn1" type="button" class="btn-primary" value="点击1" onclick="clickMe();"/>{% endblock %}{% block js %}<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script><script type="text/javascript">function clickMe() {console.log("进来了")$.ajax({url: '/task/ajax/',type: 'get',data: {n1: 123,n2: 456},success: function (res) {console.log(res);}})}</script>
{% endblock %}

views目录account.py文件

def task_list(r):"显示一个页面,用于ajax"return render(r, 'login/test.html')
def task_ajax(req):print(req.method)print(req.GET.get('n1'))return HttpResponse("成功了!")

urls.py

from django.urls import path
from appTang.views import department_views, user_views, admin_views, account# 映射关系,视图--->函数
urlpatterns = [path('task/list', account.task_list),path('task/ajax/', account.task_ajax),]

16.1 POST请求

{% block js %}<script type="text/javascript">function clickMe() {console.log("进来了")$.ajax({url: '/task/ajax/',type: 'post',data: {n1: 123,n2: 456},success: function (res) {console.log(res);}})}</script>
{% endblock %}
from django.views.decorators.csrf import csrf_exempt@csrf_exempt # 对单个函数关掉,csrf校验
def task_ajax(req):print(req.method)print(req.POST)return HttpResponse("成功了!")

16.3 绑定事件

{% block js %}<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script><script type="text/javascript">$(function (){// 页面框架加载完成之后,代码自动执行bindBtnEvent();})function bindBtnEvent(){{# 可以,发送ajax请求 #}$.ajax({url: '/task/ajax/',type: 'post',data: {n1: 123,n2: 456},success: function (res) {console.log(res);}})}</script>
{% endblock %}

16.4 ajax请求返回值

ajax 通常都是返回json格式数据

data_dict = {"status": True, "data": [11, 22, 33, 44]}
# 两种方式都可以
# json_string = json.dumps(data_dict)
# return HttpResponse(json_string)return JsonResponse(data_dict)
$.ajax({url: '/task/ajax/',type: 'post',data: {n1: 123,n2: 456},{# 将返回值,变成json对象 ,后面可以直接 .属性#}dataType: "JSON", success: function (res) {console.log(res);console.log(res.data);}
})

16.5 表单提交

两种方式,一种form,一种一个一个的val

$(“#form3”).serialize()

n1: $(“#txtUser”).val(),

{% extends 'login/layout.html' %}
{% block content %}<div><div class="container"><h3>示例2 </h3><input type="text" id="txtUser" placeholder="姓名"/><input type="text" id="txtAge" placeholder="年龄"/><input type="button" id="btn2" class="btn btn-primary" value="点击2"/><h3>示例3 </h3><form id="form3"><input type="text" name="username" placeholder="姓名"/><input type="text" name="age" placeholder="年龄"/><input type="text" name="email" placeholder="邮箱"/><input type="text" name="more" placeholder="简介"/></form><input type="button" id="btn3" class="btn btn-primary" value="点击3"/></div></div>
{% endblock %}
{% block js %}<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script><script type="text/javascript">$(function () {bindBtn2Event();bindBtn3Event();})function bindBtn2Event() {$("#btn2").click(function () {$.ajax({url: '/task/ajax/',type: 'post',data: {n1: $("#txtUser").val(),n2: $("#txtAge").val(),},dataType: "JSON",success: function (res) {console.log(res);console.log(res.status);}})})}function bindBtn3Event() {$("#btn3").click(function () {$.ajax({url: '/task/ajax/',type: 'post',data: $("#form3").serialize(),dataType: "JSON",success: function (res) {console.log(res);console.log(res.data);}})})}</script>
{% endblock %}
http://www.dtcms.com/a/442790.html

相关文章:

  • python计算化学(autode系列—xTB)Atoms类详解
  • 网站没有在工信部备案中山seo外包
  • 不通过网站可以做360全景吗网页设计与制作基础教程
  • Simulink模型转换为UPPAAL模型(2016)
  • 部署Spring Boot项目+mysql并允许前端本地访问的步骤
  • 嵌入式linux内核驱动学习4——LED
  • 建设银行人力资源招聘网站建筑行业教育培训平台
  • 蚌埠网站制作网站开发好找工作吗
  • Spring Boot 整合 MyBatis
  • 【C++实战(70)】C++ 跨平台开发:CMake构建实战指南
  • algorithm <B> data manipulation in huffman algorithm 4/99
  • 三网合一网站建设福建省建设执业资格注册中心网站
  • Rokid JSAR 技术开发全指南+实战演练
  • 昆明做网站哪家便宜计算机网络课程设计
  • 《网页设计与网站建设》A卷答案成都o2o网站建设
  • 建筑工程网官网入口商丘seo教程
  • 求解子网掩码
  • 网站 转成 微信小程序西城上海网站建设
  • 【AI论文】SLA:通过精细可调的稀疏线性注意力机制突破扩散变换器中的稀疏性局限
  • 博客自定义网站服饰 视频 网站建设
  • SSM创新实践学分管理系统08a30(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 太原建设网站制作WordPress手机号验证登录
  • SSM大学教务管理系统61dy9(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 连云港市建设工程安全监督站网站青岛平台网站建设
  • 缓存锁(Cache Lock)是什么?
  • linux建设网站php打开提示404申请一个域名可以建设一个网站吗
  • 人工智能开发工具全景指南:从编码辅助到模型部署的全链路实践
  • 做一个宣传网站要多少钱wordpress 要加上
  • mysql学习
  • 爬坑 10 年!爱回收询价接口实战:从型号匹配、分页续传到数据完整性校验