侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Django 使用Ajax进行前后台交互的示例讲解

框架(架构)  /  管理员 发布于 7年前   221

本文要实现的功能是:根据下拉列表的选项将数据库中对应的内容显示在页面,选定要排除的选项后,提交剩余的选项到数据库。

为了方便前后台交互,利用了Ajax的GET和POST方法分别进行数据的获取和提交。

代码如下:

<!--利用获取的数据进行表单内容的填充--><script>$("#soft_id").change(function(){var softtype=$("#soft_id").find("option:selected").text();var soft={'type_id':softtype}$.ajax( { type: 'GET', url:'/data/soft-filter/{{family}}', dataType: 'json', data:soft, success: function( data_get ){ build_dropdown( data_get, $( '#min_version' ), '请选择最低版本' );//填充表单 build_dropdown( data_get, $( '#max_version' ), '请选择最高版本' ); build_div(data_get,$('#soft_affected')); } });  }); var build_dropdown = function( data, element, defaultText ){ element.empty().append( '<option value="">' + defaultText + '</option>' ); if( data ){ $.each( data, function( key, value ){ element.append( '<option value="' + key + '">' + value + '</option>' ); } ); } } var build_div = function( data, element){ if( data ){ element.empty(); $.each( data, function( key, value ){  element.append(' <li class="clearfix"> <div class="todo-check pull-left"><input name="chk" type="checkbox" value="'+value+'" /></div> <div class="todo-title">'+value+' </div><div class="todo-actions pull-right clearfix"><a href="" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="todo-complete"><i class="fa fa-check"></i></a><a href="" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="todo-edit"><i class="fa fa-edit"></i></a><a href="" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="todo-remove"><i class="fa fa-trash-o"></i></a></div> </li>'); } ); }}</script>
<!--选择并提交数据--><script>//选择数据function postselect (){  var seleitem=new Array(); $("input[name='chk']").each(function(i){ if(!($(this).is( ":checked" )) ){  seleitem[i]=$(this).val();  // alert(seleitem[i]); }});//将排除后的数据提交到后台数据库var soft={'type_id':seleitem}$.ajax( { type: 'POST', url:'/data/soft-submit', dataType: 'json', data:soft, success: function( data_get ){ } });}</script>

部分html代码为:

 <div style="overflow: hidden;" >      <ul id='soft_affected' class="todo-list sortable">      </ul> </div>

views.py中处理请求和响应代码:

def soft_submit(request): if request.is_ajax():  id=request.POST.get('type_id') return HttpResponse("success")def soft_filter(request,fami): softtype='' ajax_release_version=[] release_version=[] if request.is_ajax():  softtype=request.GET.get('type_id')  soft_type=SoftTypeRef.objects.using('vul').filter(description=softtype)  soft_tp_id=0  for i in soft_type:   soft_tp_id= i.soft_type_id  web_soft=SoftWeb.objects.using('vul').filter(soft_type_id=soft_tp_id)  for i in web_soft:   ajax_release_ver=i.release_version   ajax_release_version.append(ajax_release_ver)  return HttpResponse(json.dumps(ajax_release_version), content_type='application/json')

以上这篇Django 使用Ajax进行前后台交互的示例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


  • 上一条:
    Django1.9 加载通过ImageField上传的图片方法
    下一条:
    Django+Ajax+jQuery实现网页动态更新的实例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客