zoukankan      html  css  js  c++  java
  • django HttpResponse对象

    1.服务器接收Http协议的请求后,会根据报文创建HttpResponse对象

    2.视图函数的第一个参数就是HttpResponse对象

    3.在django.http模块中定义了HttpResponse对象的API

    子路由

    from django.urls import path, re_path
    
    from . import views
    
    urlpatterns = [
        path('requ/', views.http_response, name='requ'),
    ]

    页面requestobj.html

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>httprequest对象</title>
    </head>
    <body>
        <p>{{ title }}</p>
        <form action="{% url 'requ' %}" method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            姓名:<input type="text" name="uname"><br/>
            爱好:<input type="checkbox" name="like" value="0">秘制小汉堡
                <input type="checkbox" name="like" value="1">画画baby
                <input type="checkbox" name="like" value="2">一给我里giao<br/>
                <input type="file" name="file"><br/>
                <input type="file" name="file"><br/>
            <button>提交</button>
        </form>
    </body>
    </html>

    视图函数views.py

    from django.shortcuts import render, redirect
    from django.http import HttpResponse, Http404, HttpResponseNotFound, JsonResponse
    from django.urls import reverse
    from . import models
    
    def http_response(request):
        # 判断用户是请求页面(get方法)还是提交数据(post方法)
        if request.method == 'GET':
            print(request.path)
            print(request.method)
            print(request.encoding)
            print(request.GET.get('uname', '无名'))
            print(request.GET.getlist('like', '无爱'))
            return render(request, 'requestobj.html', {'title': '填写信息'})
        elif request.method == 'POST':
            # QueryDict.get('键',default)
            #       只能获取键的一个值,如果一个键同时拥有多个值,获取最后一个值
            #       或键不存在,有default默认值则返回默认值,没有默认值则返回None
            name = request.POST.get('uname', '姓名')
            # QueryDict.getlist('键',default)
            #       将键的值以列表返回,可以获取一个键的多个值
            #       或键不存在,有default默认值则返回默认值,没有默认值则返回None
            likes = request.POST.getlist('like', '爱好')
            files = request.FILES.getlist('file', '文件')
            cookies = request.COOKIES
            print(name)
            print(likes)
            print(files)
            print(cookies)
            json_data = [{'name': name, 'likes': likes[0], 'cookies': cookies['csrftoken']},
                         {'name': name, 'likes': likes[0], 'cookies': cookies['csrftoken']}]
            # 返回json数据,一般用于异步请求,帮助用户创建JSON编码的响应,默认Content-Type为application/json
            # safe=False 当json数据为多组字典形式时需要使用
            return JsonResponse(json_data, safe=False)
  • 相关阅读:
    ios开发函数(数学函数应用)
    苹果的软件/系统盘 网站 http://www.panduoduo.net/u/bd-369186934/2
    iOS-运行时机制
    ios滑动手势全屏(这段代码实现了下一级控制器滑到上一级控制器)
    js正则表达式总结
    text-shadow属性
    css3的box-shadow属性
    white-space属性
    js的sort()方法详解
    搜索框demo
  • 原文地址:https://www.cnblogs.com/glz666/p/13773987.html
Copyright © 2011-2022 走看看