MOOC(2)-Django开发get、post请求,返回json数据
2024-10-19 11:57:19
1.对get请求直接返回参数
如果请求多个参数,也只能返回一个参数,这里只返回了username参数
如果想要返回多个参数值,可以返回json格式数据
2.对get请求返回json数据
# views.py# 对get请求返回json格式数据 from django.shortcuts import renderfrom django.http.response import HttpResponsefrom django.shortcuts import render_to_responseimport json
def Login(request): if request.method == "GET": result = {} username = request.GET.get("username") mobile = request.GET.get("mobile") data = request.GET.get("data") result["user"] = username result["mobileNum"] = mobile result["data"] = data result = json.dumps(result) return HttpResponse(result, content_type="application/json", charset="utf-8") else: return render_to_response("login.html")
测试结果如下,如果验证不生效可以重新运行一下manage.py启动项目
3.对post请求返回json数据
# views.py from django.shortcuts import render from django.http.response import HttpResponse from django.shortcuts import render_to_response import json def Login(request): if request.method == "POST": result = {} # username和password是HTML中form表单的name属性, 和HTML表单中的填写项一一对应 username = request.POST.get("username") mobile = request.POST.get("password") result["user"] = username result["mobileNum"] = mobile result = json.dumps(result) return HttpResponse(result, content_type="application/json", charset="utf-8") else: return render_to_response("login.html")
最新文章
- HTML学习-日常问题-1
- 说说设计模式~装饰器模式(Decorator)
- 各类 HTTP 返回状态代码详解
- ajx技术解析以及模拟jQuery封装
- 初探XML
- [基础] C++与JAVA的内存管理
- libeXosip2(1-3) -- How-To send or update registrations.
- vi全局替换方法
- js字符串大小写转换
- Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY'
- ServiceContract,OperationContract
- cocos2dx - 控件扩展之pageview循环显示
- django + nginx + uwsgi + websocket
- Django 路由系统(URLconf)
- Java 导出 Excel 列号数字与字母互相转换工具
- 键盘Hook【Delphi版】
- h5 扫描二维码打开app和点击下载功能的实现
- VS Code 如何直接在浏览器中预览页面
- ESXi时间同步
- [Selenium]Release in dragAndDrop doesn't work after i update the version of Selenium to 2.45.0
热门文章
- FPGA的基本组成单元LUT,以及三种核的概念
- 半监督的GAN算法
- 寒假day05-spring框架
- POJ-2349 Arctic Network(最小生成树+减免路径)
- AddressUtils
- vue slot插槽v-show不控制显示隐藏
- 《VSTO开发入门教程》配套资源下载
- play framework在eclipse中自动的预编译生成precompiled文件
- Simple Random Sampling|representative sample|probability sampling|simple random sampling with replacement| simple random sampling without replacement|Random-Number Tables
- pandas常用小trick(持续更新)