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

飞天使-通过GET 和POST进案例演示

文章目录

      • GET
      • POST

GET

 def index(request):# 在url中获取学号sno = request.GET.get("sno", None)print("学号为:",sno)# 判断学号如果有值,执行查询if sno:results = get_student_by_sno(sno)# 展示在页面return render(request, 'index.html', context={'students': results})# 没有值,返回所有学生信息else:# 读取文件信息path = r"/Users/super/Desktop/杂记/王进/1/Project/Dj010801/student/static/files/Student.txt"students = read_student_from_file(path)# 加载HTML页面return render(request, 'index.html', context={'students': students})<script>$(function () {$("#getresult").on('click',function () {// 获取文本框内容sno = $('#sno').val();// 拼接查询字符串location.href = "{% url 'home' %}?sno=" + sno;});$("#getall").on('click',function () {location.href = "{% url 'home' %}";});});</script>

POST

提交数据到服务器,返回数据,表单提交
如果要查询部分数据,等于是将部分数据提交到服务器进行查询,得到数据返回from django.http import JsonResponsedef my_post_view(request):if request.method == 'POST':# 处理POST请求的逻辑post_data = request.POST# 进行相应的处理return JsonResponse({'message': 'Post请求已收到'})else:return JsonResponse({'error': '只允许POST请求'}, status=405)fetch('/post/', {method: 'POST',headers: {'Content-Type': 'application/json',// 其他头部信息...},body: JSON.stringify({ key1: 'value1', key2: 'value2' }),
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('请求出错:', error));import requestsurl = 'http://your-domain/post/'
data = {'key1': 'value1', 'key2': 'value2'}
response = requests.post(url, data=data)
print(response.json())
http://www.lryc.cn/news/233675.html

相关文章:

  • 【MySql】12- 实践篇(十)
  • <C++> 反向迭代器
  • 【EI会议征稿】第三届网络安全、人工智能与数字经济国际学术会议(CSAIDE 2024)
  • 格力报案称“高管遭自媒体侮辱诽谤”
  • HBase之Compaction
  • 设计模式之结构型模式
  • centOs 6.10 编译 qt 5.15.11
  • Redis对象的数据结构及其原理汇总
  • @RestController 注解网页返回 [] ,出现的bug
  • C语言指针详解(1)(能看懂字就能明白系列)文章超长,慢慢品尝
  • 为什么别人年薪30W+?同样为测试人,“我“的测试之路...
  • 【Unity】XML文件的解析和生成
  • Vue h5页面手指滑动图片
  • Python类属性下划线的意义
  • DbUtils概述
  • 大数据基础设施搭建 - Hadoop
  • 测试开发环境下centos7.9下安装docker的minio
  • Django之模版层
  • spark性能调优 | 内存优化
  • 【PG】PostgreSQL高可用之自动故障转移-repmgrd
  • 操作系统OS/存储管理/内存管理/内存管理的主要功能_基本原理_要求
  • 【手写数据库toadb】SQL解析器的实现架构,create table/insert 多values语句的解析树生成流程和输出结构分析
  • 设计模式-备忘录模式-笔记
  • 机器学习—基本术语
  • pytorch单精度、半精度、混合精度、单卡、多卡(DP / DDP)、FSDP、DeepSpeed模型训练
  • 基于PHP的纺织用品商城系统
  • Go使用命令行输出二维码
  • 最长连续序列[中等]
  • 设计模式-状态模式-笔记
  • Java中for、foreach、stream区别和性能比较