• Python-Django 视图层


    1 request对象

    method:请求方式
    GET:get请求的参数(post请求,也可以携带参数)
    POST:post请求的参数(本质是从bdoy中取出来,放到里面了)
    COOKIES--->后面讲
    META:字典(放着好多东西,前端传过来的,一定能从其中拿出来)
    body:post提交的数据
    path:请求的路径,不带参数
    request.get_full_path() 请求路径,带参数
    session---后面讲
    user---后面讲
    FILES
    encoding:编码格式

    2 HttpResponse对象

    -三件套
    -JsonResponse:往前端返回json格式数据(没有它,我可以自己写)
    -转列表格式:指定safe=False
    -中文字符问题:json_dumps_params={'ensure_ascii':False}

    3 JsonResponse

    # JsonResponse
    url(r'^json$', app01_views.JasonRes),
    from django.http import JsonResponse
    def JasonRes(request):
    dic={'id':1,'name':'lll六六六','publish':'laonanhai','publish_date':'2019-01-9','author':'dj'}
    # import json
    # return HttpResponse(json.dumps(dic,ensure_ascii=False))
    return JsonResponse(dic,safe=False,json_dumps_params={'ensure_ascii':False})

     

    4 CBV(基于类的视图)和FBV(基于函数的视图)

    -cbv:一个路由写一个类
    -先定义一个类:继承自View
    from django.views import View
    class MyClass(View):
    # 当前端发get请求,会响应到这个函数
    def get(self, request):
    return render(request,'index.html')
    # 当前端发post请求,会响应到这个函数
    def post(self,request):
    print(request.POST.get('name'))
    return HttpResponse('cbv--post')
    -在路由层:
    re_path('^myclass/$',views.MyClass.as_view()),

    # CBV
    url(r'^myclass/$', app01_views.Myclass.as_view()),
    from django.views import View
    class Myclass(View):
    def get(self,request):
    return render(request,'index.html')

    def post(self,request):
    print(request.POST.get('name'),request.POST.get('pwd'))
    return HttpResponse('CBV--POST')

    5 文件上传

    -form表单默认提交的编码方式是enctype="application/x-www-form-urlencoded"
    -前端:如果要form表单上传文件,必须指定编码方式为:multipart/form-data
    -后端:
    file=request.FILES.get('myfile')
    with open(file.name,'wb') as f:
    for line in file:
    f.write(line)

    # FILES
    url(r'^file', app01_views.file_upload),
    def file_upload(request):
    if request.method=='GET':
    return render(request,'file_upload.html')
    else:
    print(request.POST)
    print(request.FILES)
    from django.core.files.uploadedfile import InMemoryUploadedFile
    file1=request.FILES.get('myfile1')
    print(type(file1))
    file2=request.FILES.get('myfile2')
    import os,time
    name=str(time.strftime("%Y-%m-%d"))+file1.name
    print(name)
    path=os.path.join('media',name)
    print(path)
    with open(path,'wb') as f:
    for line in file1:
    f.write(line)
    # with open(file2.name,'wb') as f:
    # for line in file2:
    # f.write(line)
    return HttpResponse('上传成功!')

    <body>
    <form action="" method="post" enctype="multipart/form-data">
    {#<form action="" method="post" enctype="application/x-www-form-urlencoded">#}
    <p>用户名 <input type="text" name="name"></p>
    <input type="file" name="myfile1">
    {# <input type="file" name="myfile2">#}
    <input type="submit" name="submit">
    </form>
    </body>


    6 前端提交数据编码格式:

    -multipart/form-data(上传文件)
    -application/x-www-form-urlencoded(默认编码)

    7 图书管理系统表分析:

    图书管理系统
    -表:
    book表
    author表
    publish表

    -一对一:一对多的关系一旦确立,关联字段写在哪都可以

    author=models.OneToOneField(to='Author_sim',to_field='id',on_delete=models.CASCADE)


    -一对多:一对多的关系一旦确立,关联关系写在多的一方

    publisher=models.ForeignKey(to='Publisher',to_field='id',on_delete=models.CASCADE)


    -多对多:多对多的关系,必须创建第三张表(中间表)

    authors=models.ManyToManyField(to='Author_sim')

    TTL:

    1 request对象
    -GET
    -POST
    -method
    -body
    -path:只是路径article/1/44/
    -get_full_path():带参数的路径:article/1/44/?id=1&name=12
    -META:字典,客户端的ip,客户端请求的所有东西
    -FILES:字典,客户端上传的文件字典
    -is_ajax():判断是否是ajax请求
    2 HttpResponse:只要是响应,一定是它的对象
    -render(request,'模板名字',{key:value})
    -HttpResponse()
    -redirect()
    -JsonResponse():封装了json.dumps,json_dumps_params={'ensure_ascii': False}
    3 CBV和FBV
    class Test(View):
    dispatch方法:总的分发方法
    get
    post
    路由:
    类名.as_view()
    补充:集群和分布式
    4 文件上传:
    前端:form提交:指定编码格式from-data
    后台:file=request.FILES.get('名字')
    -打开空文件,用for循环读file,写入空文件

    5 图书表设计
    -一对一:关联关系,写在谁中都可以
    -一对多:关联关系写在多的一方
    -多对多:必须创建中间表
    -orm
    -一对一:OneToOneField(to=表,to_field=字段)
    -一对多:ForeignKey(to='Publish', to_field='id',on_delete=models.CASCADE)
    -多对多:models.ManyToManyField(to='Author')自动创建第三张表
    -DateField()
    -DecimalField()
    -IntegerField()

    TTL2

     

  • 相关阅读:
    JVM系列五:JVM监测&工具[整理中]
    Washing Text Animation
    Cloth
    藤条生长为字母的动画
    音频驱动曲线
    Wood Chipping Text Animation
    融化的文字
    流体文本动画
    Blender Tutorial
    UV纹理+修改器:VertexWeightEdit+修改器:Mask遮罩
  • 原文地址:https://www.cnblogs.com/du-jun/p/10246276.html
Copyright © 2020-2023  润新知