1 虚拟环境:解决问题同一台机器上可以运行不同版本的django,
  1 用pychanrm创建--->files-->newproject--->选择虚拟环境
  2 settings-->project创建
  3 用命令行创建,详见https://www.cnblogs.com/liuqingzheng/p/9508851.html
 2 django 2.0和django 1.0 路由层区别(*****url,re_path分组分出来的数据,是字符串)
  -re_path:跟1.0的url用法相同
  -path:传的路径,是准确路径
   5个转换器-->path('test/<path:year>', views.re_test),视图函数记得传参year
   str,匹配除了路径分隔符(/)之外的非空字符串,这是默认的形式
   int,匹配正整数,包含0。
   slug,匹配字母、数字以及横杠、下划线组成的字符串。
   uuid,匹配格式化的uuid,如 075194d3-6885-417e-a8a8-6c931e272f00。
   path,匹配任何非空字符串,包含了路径分隔符(/)
  -自定义转换器
   1 定义:一个类:
    class MyCon:
     # 写一个正则表达式
     regex = '[0-9]{4}'
     # 匹配出來的数据,会传到这里,retrun回去的,会被视图函数接收
     def to_python(self, value):
      return int(value)
     # 反向解析用的
     def to_url(self, value):
      return '%04d' % value
   2注册: from django.urls import register_converter
     register_converter(MyCon,'yyy')
   3 使用:path('test/<yyy:year>', views.re_test,name='test'),
 2.1补充:
  # 为假,不会加反斜杠
  # APPEND_SLASH=False
 3 视图层之HttpRequest对象
  # 前台Post传过来的数据,包装到POST字典中
  # request.POST
  # 前台浏览器窗口里携带的数据,包装到GET字典中
  # request.GET
  # 前台请求的方式
  # request.method
  # post提交的数据,body(请求/响应)体的内容,前台会封装成:name=lqz&age=18&sex=1
  # request.body
  # 取出请求的路径,取不到数据部分
  # print(request.path)
  # 取出请求的路径,能取到数据部分
  # print(request.get_full_path())
  #请求/响应 头的东西
  # print(request.META)
 4 视图层之HttpResponse对象
  三件套:render,HttpResponse,redirect
  render函数:
   temp=Template('<h1>{{ user }}</h1>')
   con=Context({'user':'lqz'})
   ret=temp.render(con)
   print(ret)
   return HttpResponse(ret)
  
   # return render(request,'index.html')的内部
  
 5 视图层之JsonResponse对象
  -导入:from django.http import JsonResponse
  -视图函数中:
   def test(request):
    import json
    # dic={'name':'lqz','age':18}
    ll = ['name', 'age']
    # 把字典转换成json格式,返回到前台
    # return HttpResponse(json.dumps(dic))
    # 把列表转换成json格式,返回到前台
    # return HttpResponse(json.dumps(ll))
    # 把字典转换成json格式,返回到前台
    # return JsonResponse(dic)
    # 报错,默认不支持列表形式
    # return JsonResponse(ll)
    # 支持列表形式
    return JsonResponse(ll,safe=False)
 6 CBV和FBV
  -基于类的视图
   -1 路由层:url(r'^test/', views.Test.as_view()),
   -2 视图层
    -导入:from django.views import View
    -写一个类:
     class Test(View):
      def get(self, request):#一定要传request对象
       return HttpResponse('get-test')
      def post(self, request):
       return HttpResponse('post-test')
  -基于函数的视图   
  
 7 简单文件上传
  前端:
   <form action="" method="post" enctype="multipart/form-data">
   {#<form action="" method="post" enctype="application/x-www-form-urlencoded">#} 这两种一个是默认 一个是上传文件 都可以获取POST内的数据
    <input type="file" name="myfile">
    <input type="text" name="password">
    <input type="submit" value="提交">
   </form>
  后台:
   def fileupload(request):
    if request.method=='GET':
     return render(request,'fileupload.html')
    if request.method=='POST':
     # FILES
     print(request.FILES)
     print(type(request.FILES.get('myfile')))
     # 从字典里根据名字,把文件取出来
     myfile=request.FILES.get('myfile')
     from django.core.files.uploadedfile import InMemoryUploadedFile  这行是:查看源码用
     # 文件名字
     name=myfile.name
     # 打开文件,把上传过来的文件存到本地
     with open(name,'wb') as f:
      # for line in myfile.chunks():
      for line in myfile:
       f.write(line)
     return HttpResponse('ok')
 
  补充:*****编码方式multipart/form-data或者:application/x-www-form-urlencoded传的数据,都可以从POST中取出来

随机推荐

  1. Openresty 学习笔记(一)opm 工具的使用

    1.自1.11.2.2开始,OpenResty版本已经包含并默认安装opm.所以通常你不需要自己安装opm. 2.我们在这里只需要做一个软连接就可以了 cd /usr/local/openresty/ ...

  2. Vue.Draggable/SortableJS 的排序功能,在VUE中的使用

    此插件git: https://github.com/SortableJS/Vue.Draggable 基于Sortable.js http://www.cnblogs.com/xiangsj/p/6 ...

  3. 微信接口 output {"errMsg":"translateVoice:fail, the permission value is offline verifying"}

    jsApiList : [ 'checkJsApi', 'startRecord', 'stopRecord','translateVoice','scanQRCode', 'openCard' ]增 ...

  4. Multiple vulnerabilities in DASAN H660RM GPON router firmware

    CVE-2019-9974: diag_tool.cgi on DASAN H660RM devices with firmware 1.03-0022 allows spawning ping pr ...

  5. @Component注解的解析

    今天在写程序的时候看见一个以前没有见过的注解(@Component),在网上查找过后,经过实践,决定把它记录下来. 1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中 ...

  6. react路由的安装及格式和使用方法

    react路由的安装: 在要创建项目的目录命令窗里输入: cnpm install -g create-react-app create-react-app  项目名 在创建好的项目目录命令窗里输入: ...

  7. 如何设置vmware 让别人连接你的虚拟机

    参考链接: http://blog.csdn.net/yuan1590/article/details/53504042 http://blog.csdn.net/u012110719/article ...

  8. ssm框架所需jar包整理及各jar包的作用

    以下是我目前新搭建的ssm项目的pom.xml 之后如果需要其他的话再加 <?xml version="1.0" encoding="UTF-8"?> ...

  9. 【tmos】spring boot项目中处理Schedule定时任务

    我的代码 /** * Author:Mr.X * Date:2017/10/30 14:54 * Description: */ @Component @Configurable @EnableSch ...

  10. lambda创建匿名函数

    1)print map(lambda x: x + 1, [y for y in range(10)]) 输出:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]map(lambda &l ...