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")

  

最新文章

  1. HTML学习-日常问题-1
  2. 说说设计模式~装饰器模式(Decorator)
  3. 各类 HTTP 返回状态代码详解
  4. ajx技术解析以及模拟jQuery封装
  5. 初探XML
  6. [基础] C++与JAVA的内存管理
  7. libeXosip2(1-3) -- How-To send or update registrations.
  8. vi全局替换方法
  9. js字符串大小写转换
  10. Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY'
  11. ServiceContract,OperationContract
  12. cocos2dx - 控件扩展之pageview循环显示
  13. django + nginx + uwsgi + websocket
  14. Django 路由系统(URLconf)
  15. Java 导出 Excel 列号数字与字母互相转换工具
  16. 键盘Hook【Delphi版】
  17. h5 扫描二维码打开app和点击下载功能的实现
  18. VS Code 如何直接在浏览器中预览页面
  19. ESXi时间同步
  20. [Selenium]Release in dragAndDrop doesn't work after i update the version of Selenium to 2.45.0

热门文章

  1. FPGA的基本组成单元LUT,以及三种核的概念
  2. 半监督的GAN算法
  3. 寒假day05-spring框架
  4. POJ-2349 Arctic Network(最小生成树+减免路径)
  5. AddressUtils
  6. vue slot插槽v-show不控制显示隐藏
  7. 《VSTO开发入门教程》配套资源下载
  8. play framework在eclipse中自动的预编译生成precompiled文件
  9. Simple Random Sampling|representative sample|probability sampling|simple random sampling with replacement| simple random sampling without replacement|Random-Number Tables
  10. pandas常用小trick(持续更新)