[django]form的content-type(mime)

form默认的content-type是

'application/x-www-form-urlencoded'

可以修改为多文档: 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的断点调试工具

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

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
posted @ 2018-08-23 11:04  mmaotai  阅读(356)  评论(0编辑  收藏  举报