Template Import tab _cookie_ decorator _ _ notes

默认值:
url(r'index/', views.index,{'name':"root"})
def index(request,name):
    print(name)
    return HttpResponse('ok')


命名空间:
/a/   include('app01.urls',namespace='m1')
/b/   include('app01.urls',namespace='mm1')

app01.urls
/index/  name='n1'

reverse('m1:n1')-->对应url /a/index/

命名空间补充:
from django.conf.urls import url,include
urlpatterns = [
    url(r'^a/', include('app01.urls', namespace='author-polls')),
    url(r'^b/', include('app01.urls', namespace='publisher-polls')),
]

app01.urls.py
from django.conf.urls import url
from app01 import views
= APP_NAME 'app01'
the urlpatterns = [
    URL (R & lt '? ^ (P <PK> \ + D) / $', views.detail, name = 'Detail')
]

app01.views.py
DEF Detail (Request, PK):
    Print (request.resolver_match)
    return the HttpResponse (PK)

as defined above with a namespace after url, when used to generate the URL name, should be as follows:
V = Reverse ( 'polls-author: Detail', kwargs = { 'PK':. 11} )
{% URL 'polls-author: Detail' PK = PP = 99%} 12 is



a custom function
    simple_tag
    under 1 app directory created templatetags
    2 py arbitrary file xxoo.py

    . 3 created template objects, Register

      from Django Import template
      from django.utils Import mark_safe .safestring

      Register = template.Library ()

    4.
      @Register.simple_tag
      HOU DEF (A1, A2):
        return A1 + A2

    5. The Settings Register App

    6. The top XXOO% Load%} {

    7. The function name using {% arg1 arg2%} pass any parameters
            {% hou 2 9%}

    disadvantages : not as if condition
    bit : Set parameters




    filter
    created under the directory 1 app templatetags
    2 py arbitrary file xxoo.py

    . 3 created template objects, Register

      from Django Import template
      from django.utils.safestring Import mark_safe

      Register = template.Library ()

    4.
      @Register .filter
      DEF HOU (A1, A2):
        return A1 + A2

    5. The Settings Register App

    6. The top XXOO% Load%} {

    7. The use {{ "a parameter" | function name: "parameters two, three parameters "}} or {{" a parameter "| function name:}} digital any parameters passed
            {{ "Maliya" | jz: "LSS"}}

    disadvantage : up to two parameters can not be spaces
    advantages : if the condition can be used as


tab (custom paging):
    XSS:
        front end : {{page_str | safe}}

        Background : mark_safe (page_str)




Cookie
key, key
value = '', the value
max_age = None, timeout
expires = None, timeout (IEs Expires the requires, IF IT SO SET has already been Not.)
path = '/', cookies in force path, / represents the root path, special: with cookie path can be url to access any page of
the domain name to take effect = None of the domain, cookie
Secure = False, HTTPS transmission
httponly = False only http protocol transmission, JavaScript can not be acquired (not absolute, can capture the bottom may also be acquired cover)



the auth decorators
DEF the auth (FUNC):
    DEF Inner (Request, * args, ** kwargs):
        v=request.COOKIES.get('username111')
        if not v:
            return redirect('/login/')
        return  func(request,*args,**kwargs)
    return inner

FBV装饰
@auth
def index(request):
    #获取当前已经登录的用户
    v=request.COOKIES.get('username111')
    return render(request,'index.html',{'current_user':v})


CBV装饰
from django import views
from django.utils.decorators import method_decorator
@method_decorator(auth,name='dispatch')
class Order(views.View):
    # @method_decorator(auth)
    # def dispatch(self, request, *args, **kwargs):
    #     return super(Order,self).dispatch(request,*args,**kwargs)

    @method_decorator(auth)
    def get(self,request):
        v = request.COOKIES.get('username111')
        return render(request, 'index.html', {'current_user': v})

    def post(self,request):
        v = request.COOKIES.get('username111')
        return render(request, 'index.html', {'current_user': v})

Guess you like

Origin www.cnblogs.com/leiwenbin627/p/11099296.html