Form驗證筆記

views
    request.body
        request.POST(request.body)
        request.FILES(request.body)
        request.GET
        request.XXX.getlist

    request.Meta(...)
        request.method(POST,GET,PUT)
        request.path_info
        request.COOKIES


返回cookie
    response=HttpResponse(a)
    response.set_cookie()
    return response

    response['name']='alex' 建立響應頭 name=alex


model操做
    表內容操做:
        models.TB.objects.create()
        models.TB.objects.create(**{})
        obj=models.TB(...)
        obj.save()

        models.TB.objects.all()
        models.TB.objects.all()[7:10]  切片數據

        models.TB.objects.update(...)
        models.TB.objects.filter(id__in=[1,2,3...])
        models.TB.objects.filter(單下劃線id)
        models.TB.objects.filter(..).delete
        models.TB.objects.values  結果是字典
        models.TB.objects.values_list 結果是元組
        models.TB.objects.get
        models.TB.objects.filter().update()
        models.TB.objects.filter().first()
        models.TB.objects.filter(**{})
        models.TB.objects.filter(**{}).count()
        models.TB.objects.filter(雙下劃線跨表)
        models.TB.objects.filter(id__gt=1,name='root')  and關係
        models.TB.objects.filter(id__range=[1,3])
        ...                     (id__lt=1)
                                (id__lte=1)
                                (id__gte=1)

        models.TB.objects.exclude(id__gt=1)

        多對多:
            obj.set
            obj.add(1,2,3)
            obj.add([1,2,3])
            obj.remove([1,2,3])
            obj.clear()
            obj.all()

         models.TB.objects.all()
         [obj,obj]
         obj.fk.name

         models.TB.objects.all().order_by('')
         models.TB.objects.distinct()


        class A:
            name ..
            # b_set  經過A查B

        class B:
            caption ..
            fk=ForignKey(A)
            B.fk.A的列名  經過B查A


        多對多
         class A:
            name ..
            # b_set  經過A查B

        class B:
            caption ..
            r=M2M(a)


模板語言
        def func(request):
            return render(request,'index.html',{'val':[1,2,3]})

        index.html
            <h1>{{val.0}}</h1>

         繼承
         extends  "layout.html"


        include加載組件

        simple_tag,filter


session
    基於 cookie作用戶驗證時:敏感信息不適合放在cookie中
    cookie:保存在用戶瀏覽器端的鍵值對
    session:保存在服務器的鍵值對 依賴於cookie
    原理:生成一個隨機字符串對應一個字典,value保存用戶信息

    服務器session
        獲取session
        request.session.get()
        設置session
        request,session[x]=x
        清空session
        request.session.clear()

    def使用
        # 獲取、設置、刪除Session中數據
        request.session['k1']
        request.session.get('k1',None)
        request.session['k1'] = 123
        request.session.setdefault('k1',123) # 存在則不設置
        del request.session['k1']

        # 全部 鍵、值、鍵值對
        request.session.keys()
        request.session.values()
        request.session.items()
        request.session.iterkeys()
        request.session.itervalues()
        request.session.iteritems()

        # 用戶session的隨機字符串
        request.session.session_key

        # 將全部Session失效日期小於當前日期的數據刪除
        request.session.clear_expired()

        # 檢查 用戶session的隨機字符串 在數據庫中是否
        request.session.exists("session_key")

        # 刪除當前用戶的全部Session數據
        request.session.delete("session_key")

        request.session.set_expiry(value)
            * 若是value是個整數,session會在些秒數後失效。
            * 若是value是個datatime或timedelta,session就會在這個時間後失效。
            * 若是value是0,用戶關閉瀏覽器session就會失效。
            * 若是value是None,session會依賴全局session失效策略。


    配置文件設置默認操做:
    settings.py配置
    SESSION_COOKIE_NAME = "sessionid"                       # Session的cookie保存在瀏覽器上時的key,即:sessionid=隨機字符串(默認)
    SESSION_COOKIE_PATH = "/"                               # Session的cookie保存的路徑(默認)
    SESSION_COOKIE_DOMAIN = None                             # Session的cookie保存的域名(默認)
    SESSION_COOKIE_SECURE = False                            # 是否Https傳輸cookie(默認)
    SESSION_COOKIE_HTTPONLY = True                           # 是否Session的cookie只支持http傳輸(默認)
    SESSION_COOKIE_AGE = 1209600                             # Session的cookie失效日期(2周)(默認)
    SESSION_EXPIRE_AT_BROWSER_CLOSE = False                  # 是否關閉瀏覽器使得Session過時(默認)
    SESSION_SAVE_EVERY_REQUEST = False                       # 是否每次請求都保存Session,默認修改以後才保存(默認)每次請求 超時時間起點從新計算



緩存
CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
            'LOCATION': [
                ('172.19.26.240:11211',10),
                ('172.19.26.242:11211',11),
            ]
        }
    }


信號
Model signals
    pre_init                    # django的modal執行其構造方法前,自動觸發
    post_init                   # django的modal執行其構造方法後,自動觸發
    pre_save                    # django的modal對象保存前,自動觸發
    post_save                   # django的modal對象保存後,自動觸發
    pre_delete                  # django的modal對象刪除前,自動觸發
    post_delete                 # django的modal對象刪除後,自動觸發
    m2m_changed                 # django的modal中使用m2m字段操做第三張表(add,remove,clear)先後,自動觸發
    class_prepared              # 程序啓動時,檢測已註冊的app中modal類,對於每個類,自動觸發
Management signals
    pre_migrate                 # 執行migrate命令前,自動觸發
    post_migrate                # 執行migrate命令後,自動觸發
Request/response signals
    request_started             # 請求到來前,自動觸發
    request_finished            # 請求結束後,自動觸發
    got_request_exception       # 請求異常後,自動觸發
Test signals
    setting_changed             # 使用test測試修改配置文件時,自動觸發
    template_rendered           # 使用test測試渲染模板時,自動觸發
Database Wrappers
    connection_created          # 建立數據庫鏈接時,自動觸發

導入信號模塊
    from django.core.signals import request_finished
    from django.core.signals import request_started
    from django.core.signals import got_request_exception

    from django.db.models.signals import class_prepared
    from django.db.models.signals import pre_init, post_init
    from django.db.models.signals import pre_save, post_save
    from django.db.models.signals import pre_delete, post_delete
    from django.db.models.signals import m2m_changed
    from django.db.models.signals import pre_migrate, post_migrate

    from django.test.signals import setting_changed
    from django.test.signals import template_rendered

    from django.db.backends.signals import connection_created


    def callback(sender, **kwargs):
        print("xxoo_callback")
        print(sender,kwargs)

    xxoo.connect(callback)
    # xxoo指上述導入的內容



自定義信號
a. 定義信號
import django.dispatch
pizza_done = django.dispatch.Signal(providing_args=["toppings", "size"])

b. 註冊信號
def callback(sender, **kwargs):
    print("callback")
    print(sender,kwargs)

pizza_done.connect(callback)

c. 觸發信號
from sg import pizza_done

pizza_done.send(sender='seven',toppings=123, size=456)

html

相關文章
相關標籤/搜索