[django]form的content-type(mime)

form默认的content-type是

1312420-20180823110159894-468914710.png

'application/x-www-form-urlencoded'

1312420-20180823110130047-1762588628.png

可以修改为多文档: enctype即为mime类型

    <form action="/login/" method="post" enctype="multipart/form-data">
        <input type="text" name="username" placeholder="username" autocomplete="off"> {{ login_form.errors.username }}
        <br>
        <input type="text" name="password" placeholder="password" autocomplete="off"> {{ login_form.errors.password }}
        <br>
        <input type="submit">

        {% csrf_token %}
    </form>

注: 这里我用django的断点调试工具
1312420-20180823111233682-1410432157.png

mime基础知识

https://www.cnblogs.com/iiiiiher/p/8406314.html

- 每一种资源文件都有自己的标识类型. 浏览器按照提供的MIME(content-type)类型渲染页面
text/plain  txt

    var suffix = reg.exec(pathname)[1].toUpperCase();
    var reg = /\.(HTML|CSS|JS|JSON|TXT|ICO)/i;
    var suffixMIME = 'text/plain';
    if (reg.test(pathname)) {
        switch (suffix) {
            case "HTML":
                suffixMIME = 'text/html';
                break;
            case "JS":
                suffixMIME = 'text/javascript';
                break;
            case "CSS":
                suffixMIME = 'text/css';
                break;
            case "JSON":
                suffixMIME = 'application/json';
                break;
            case "ICO":
                suffixMIME = 'application/octet-stream';
                break;
        }
    }

JsonResponse添加mime

1312420-20180823112544242-1370734022.png

from django.http import HttpResponse,JsonResponse

def home(request):
    data = {
        'name': 'maotai',
        'age': 22
    }
    import json
    return HttpResponse(json.dumps(data), content_type='application/json', status=400)


def home2(request):
    data = {'name': 'maotai', 'age': 23}
    return JsonResponse(data, safe=True)

JsonResponse(继承HttpResponse)基于JsonResponse做了一层封装,返回数据浏览器直接json mime显示.
1,data.dumps
2,加上了content_type

3,JsonResponse支持序列化列表,字典.

def home2(request):
    arr = [1,2,3]
    return JsonResponse(data, safe=False) # safe值默认是true, 如果是列表,则safe置为False, 如果是字典为true.

mime

http://www.ruanyifeng.com/blog/2008/06/mime.html
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types

常见的MIME类型
    超文本标记语言文本 .html,.html text/html 
    普通文本           .txt text/plain 
    RTF文本            .rtf application/rtf 
    GIF图形            .gif image/gif 
    JPEG图形           .jpeg,.jpg image/jpeg 
    au声音文件         .au audio/basic 
    MIDI音乐文件       .mid,.midi audio/midi,audio/x-midi 
    RealAudio音乐文件  .ra, .ram audio/x-pn-realaudio 
    MPEG文件           .mpg,.mpeg video/mpeg 
    AVI文件            .avi video/x-msvideo 
    GZIP文件           .gz application/x-gzip 
    TAR文件            .tar application/x-tar

其他: http://www.cnblogs.com/CraryPrimitiveMan/p/4337351.html

转载于:https://www.cnblogs.com/iiiiiher/p/9522569.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值