zoukankan      html  css  js  c++  java
  • django 基础1

    1.web应用

    本质是基于socket实现的应用程序

    浏览器---------服务器

    2.http协议:应用层协议

    1.基于TCP协议

    2.基于请求响应

    3.短连接

    4.无状态

    请求协议

    浏览器---------->服务器  请求协议

    服务器----------->浏览器  响应协议

    3.请求协议

    请求首行 get path?get数据  HTTP/1.1

    请求头

    userAgent :win Chorome/IPhone (在爬虫中标识来源)

    contentType:json  application/x-www-form-urlencoded(用来告诉服务器数据类型,不告诉默认url)

    空行

    请求体(post才有请求体) request.body获取数据 WSGR解析不了,返回的一串数据

    a=1$b=2

    {"a":1,"b":2}

    如何给服务器发送json数据
                给服务器发送请求方式

    1.地址栏  get请求

    2.form表单 get  post请求(无法发送json数据) 因为没有方法告诉浏览器是json数据 contentType没有

    3 a标签连接请求    get请求

    4 Ajax请求   get(查)  post(提交数据添加记录)  put(更新)  delete(删除) ......

    $.ajax({
                                 url:"/index/",
                                 type:"post",
                                 data:{
                                    a:1,
                                    b:2
                                 },  # 默认urlencoded编码
                                 success:function(res){

    })

    发送json数据


                            $.ajax({
                                 url:"/index/",
                                 type:"post",
                                 contentType:"json"#告诉服务器,发送的是json数据,
                                 data:JSON.stringfy({#将字典数据解析为json数据
                                    a:1,
                                    b:2
                                 }),
                                 success:function(res){
                                     
                                
                                 }
                            
                            })

    注意,Django解析不了json数据,只能自己利用request.body解析

               
        响应协议
                响应首行    HTTP/1.1  200  OK
                响应头
                contentType:"json"
                ...
                空行
                响应体    

                响应状态码:1开头 :请求中

    200:请求成功 3开头:重定向 4:文件路径找不到 5:服务器错误

    Django

    MTV+url分发

    反响解析:url(r'^role/add/$', views.role, name='role_add'),
                    url(r'^role/edit/(d+)/$', views.role, name='role_edit'),

           视图:
                        
                        path=reverse("role_add")  # "role/add/"
                        path=reverse("role_edit",args=(1,2))  # "role/edit/1/"
                   
                   
                   模板:
                       {% url 'role_add' %}
                       {% url 'role_edit' 2 %}
              

    名称空间:用来做分发时,为了避免reverse的名称发生重复,来使用
                re_path(r'^app01/', include(("app01.urls","app01"),namespace="app01",)),

    视图函数:

    request 对象:储存这次请求所有请求信息

    属性:

    HttpRequest.GET
                     HttpRequest.POST
                                ---- urlencoded编码
                                ---- 注意:键值对的值是多个的时候,比如checkbox类型的input标签,select标签,需要用:
                                            request.POST.getlist("hobby")   

    request.body(一个字串,代表请求头报文的主体)

    request.method 请求方式

     HttpRequest.META  请求头
                     HttpRequest.FILES
                     HttpRequest.COOKIES
                     HttpRequest.session    session中间件 源码
                     HttpRequest.user       认证中间件 源码

    方法: request.get_full_path (获取带上访问参数的请求?a=1&b=2)

    rqquest.is_ajax(判断是否为ajax请求)

     HttpResponse对象:

    obj=HttpResponse("hello")
                    obj=render(request,"index.html")
                    obj=redircet("/index/")

    FBV CBV *****源码流程

    views:
                      from django.views import View
                      class BookView(View):
                            
                            def get(self,request):
                                    pass

                            def post(self,request):
                                    pass                                
                urls.py:
                       url("books",BookView.as_view())        
    模板层:
    含有模板语法的html文件为模板文件 template 里面就是模板文件

    render 方法渲染引擎

    模板语法:

    {{} }渲染变量   深度查询 句点符

    过滤器 date,safe,add,slice

    {% %}:渲染标签            
                     {%for i in [111,222,333]%}
                           {%if i!= 222%}
                               <p>i</p>
                           {%endif%}
                     {%endfor%}   

    自定义标签和过滤器
                      1、在settings中的INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag.            
                      2、 在app中创建templatetags模块(模块名只能是templatetags)
                      3、 创建任意 .py 文件,如:my_tags.py

    from django import template

    register = template.Library()

    @register.filter
                                def filter_multi(v1,v2):
                                    return  v1 * v2
                                    
                                @register.simple_tag
                                def simple_tag_multi(v1,v2):
                                    return  v1 * v2
       4、模板中使用:

    {% load my_tags%}

    num=12

    {{num|filter_multi:2}}

    {{ num|filter_multi:"[22,333,4444]" }}


                继承 extend:
                        创建base.html:
                            构建钩子
                            {%block css%}
                                
                            {%endblock css%}
                            
                            {%block content%}
                                <p>123</p>
                            {%endblock%}
                            
                            {%block js%}
                                
                            {%endblock js%}

    子模板继承:
    {%extend "base.html"%}

    {% block content%}

    <p>111<p>

    {%endblock%}

  • 相关阅读:
    B+树的Copy-on-Write设计
    so库链接和运行时选择哪个路径下的库?
    Xapian索引-文档检索过程分析之匹配百分比
    Xapian索引-文档检索过程分析
    Xapian的内存索引-添加文档
    Xapian的内存索引
    Xapian使用入门
    一个std::sort 自定义比较排序函数 crash的分析过程
    编译GCC4.8.2
    使用C++11的一点总结
  • 原文地址:https://www.cnblogs.com/zhangqing979797/p/10004249.html
Copyright © 2011-2022 走看看