路由系统

学习地址:http://www.cnblogs.com/liwenzhou/articles/8305104.html

0.Django内置sqlite3数据库的使用

  1.sqlite:是一种文件数据库,使用单独一个文件保存数据

  2.多用于嵌入式,该文件中类似于MySQL也存在很多表

  3.Django的settings.py中默认使用的就是sqlite3,不需要自己配置

  4.Pycharm连接sqlite3,需要下载驱动

1.FBV和CBV(Function Base View和Class Base View)

  1.基于函数的视图:FBV

    视图函数中通过request.method的不同进而执行不同的代码

  2.基于类的视图:CBV

    1.视图类必须继承django.views.View

    2.在类中定义和请求方法同名的方法

    3.urls.py中注册视图的时候区别于FBV要写成类名.as_view()

2.request对象的属性和方法

  1.request.method

  2.request.path_info  -->获取URL路径(不带参数)

  3.request.get_full_path  -->获取URL带参数()

  4.request.GET

  5.request.POST

    1.request.POST.get()

    2.request.POST.getlist()

  6.request.FIELS

  7.request.body

  8.request.META

3.上传文件

  1.form表单需要加enctype="multipart/form-data"

  2.request.FILES  -->大字典

  3.在后端自行写代码保存上传的文件

    with open(filename_obj

    name,"wb") as f:

      for i in filename_obj.chunks():

        f.write(i)

4.response:指render,redirect,HttpResponse

  1.基础必会三件套

    from django.shortcuts import HttpResponse,render,redirect

  2.JsonResponse

    

def json_data(request):
    d = {'黎明':'sdf','age':45}
    # import json
    # str = json.dumps(d,ensure_ascii=False)
    # return HttpResponse(str)

    from django.http import JsonResponse
    return JsonResponse(d)

  

5.路由新系统:http://www.cnblogs.com/liwenzhou/p/8271147.html

  1.正则表达式

    1.Django路由系统匹配URL路径的时候是从上到下按照注册顺序来的

    2.Django的路由系统只匹配URL路径,不匹配域名,端口,URL参数

    3.分组匹配    --> 相当于给视图函数传位置参数

    4.分组命令匹配  -->相当于给视图函数传关键字参数

    5.注意

      分组匹配和分组命名 匹配不能混合使用!!

  2.给URL匹配模式起名字

    1.name='别名'    -->起别名的目的是为了反向解析URL的时候有依据

  3.通过名字反向解析

    1.在视图函数中:

      from django.urls import reverse

        reverse('别名')

    2.在模板HTML页面

      {% url '别名' '参数1' ... %}

  4.namespace模式

    #所有以app01开头的都交给app01/urls.py去处理

      url(r'^app01/',include(app01_urls,namespace='app01')),

    #所有以xiayuhao开头的都交给app02/urls.py去处理

      url(r'^app02/',include(app02_urls,namespace='app02')),

 

 

    

 

  

posted @ 2018-11-05 17:32  俏如來  阅读(118)  评论(0编辑  收藏  举报