django FBV && CBV upload-file json reverse lookup routing system

FBV and CBV

Reference Site

https://www.cnblogs.com/liwenzhou/articles/8305104.html#autoid-1-1-0

The request object

Reference Site

https://www.cnblogs.com/liwenzhou/articles/8305104.html#autoid-2-0-2

Upload file example

def upload(request):
    """
    保存上传文件前,数据需要存放在某个位置。默认当上传文件小于2.5M时,django会将上传文件的全部内容读进内存。从内存读取一次,写磁盘一次。
    但当上传文件很大时,django会把上传文件写到临时文件中,然后存放到系统临时文件夹中。
    :param request: 
    :return: 
    """
    if request.method == "POST":
        # 从请求的FILES中获取上传文件的文件名,file为页面上type=files类型input的name属性值
        filename = request.FILES["file"].name
        # 在项目目录下新建一个文件
        with open(filename, "wb") as f:
            # 从上传的文件对象中一点一点读
            for chunk in request.FILES["file"].chunks():
                # 写入本地文件
                f.write(chunk)
        return HttpResponse("上传OK")

#上传文件示例代码

django json format output

view.py, view

def json(request):
    from django.http import JsonResponse
    from json import dumps
    data = {
        '素还真' : "半神半圣亦半仙",
        '一叶书' : "笑尽英雄",
        '叶小钗' : "啊",
    }

    data_str = dumps(data,ensure_ascii=False,sort_keys=True, indent=4, separators=(',', ':'))
    return  HttpResponse(data_str,content_type="application/json")
    # return  HttpResponse(data_str,)

    # return JsonResponse(data)

django upload files

def upload(request):
    if request.method == "POST":
        # 获取文件名称
        filename =request.FILES['filename'].name
        print(filename)
        # 获取文件上传集合
        print(request.FILES)
        # 取上传的单个文件进行写入操作
        with open(filename, "wb") as f:
            # 以每个chunks为单元写入磁盘
            for i in request.FILES['filename'].chunks():
                f.write(i)
        #         返回响应值
        return HttpResponse("上传OK")
        # return HttpResponse("ok")
    else:
        return render(request,'user/upload.html')

Routing System

http://www.cnblogs.com/liwenzhou/p/8271147.html

  1. Fuzzy matching regular expression
  2. Packet matches -> corresponding to the position parameter passed to the view function
  3. Packet name-matching -> keyword arguments passed to the view corresponding to the function
    (two do not mix)

Reverse lookup URL

In essence, to the url pattern matching aliases, and then used the alias to get a specific URL path

  1. How aliases?
    In the url matching pattern, the definition name = "Alias"
  2. how to use?
    1. In which language template:
      {% URL "alias"%} -> specific URL path obtained
    2. How to use in the view:
      from django.urls Import Reverse

      Reverse ( "alias") -> to give specific URL path
  3. How to pass parameters?
    1. Template language:
      {% url "Alias" 2018 "nb"%}
    2. In view function
      setter position parameters:
      Reverse ( "alias", args = (2018, " nb"))

      Keyword parameters passed:
      Reverse ( "alias" kwargs = { "year": 2018, "title": "nb"})
  4. namespace
    to prevent different url app following pattern matching duplicate alias

Guess you like

Origin www.cnblogs.com/anyux/p/11922039.html