django 笔记3

FBV
    function base view

    url.py
        index -> 函数名
    view.py
        def 函数(requset):
            ...

CBV
    class base view
        /index/ -》 函数名
        /index/ -> 类 方法
######################################
from django.views import View
class Home(View):
    def dispatch(self, request, *args, **kwargs):
        #调用父类中的dispatch  此方法可以类比为装饰器
        print('before')
        result = super(Home,self).dispatch( request, *args, **kwargs)
        print('after')
        return result

    def get(self,request):
        print(request.method)
        return render(request,'home.html')

    def post(self,request):
        print(request.method)
        return render(request, 'home.html')
####################################################
    请求头
Request URL:http://127.0.0.1:8000/home/?
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:8000

############################################
from django.conf.urls import url
from app01 import views
urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^index/', views.index),
    url(r'^login/', views.login),
    url(r'^home/', views.Home.as_view())
]
############################################
dispatch()方法 用来反射找到get/post方法


5.装饰器

路由系统
########################################
        {% for y in user_dict.values %}  {% for y in user_dict.keys %} {% for x,y in user_dict.items %}
        <li>{{y}}</li>
        {% endfor %}

        python for i in user_dict.keys():
        python for i in user_dict.values():
        python for i,j in user_dict.items():

路由系统,URL
    1、url(r'^index/', views.index), url(r'index/', Views.Home.as_view()),  /*from django.views import View*/

    2、url(r'^index-(\d+).html', Views.detail),  正则 一类 url对应一个函数 开发了个查看详细页面
    def detail(request, nid):
        detail_info = USER_DICT[nid]
        return render(request,'detail.html', {'detail_info':detail_info})

    3、分组 根据?P<nid>
    url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html', views.detail), #分组
    def detail(request, nid, uid):  (#?P<nid>\d+)  (?P<uid>\d+) 此时就与nid uid存放位置无关啦
        pass
    def detail(request, *args, **kwargs):  #这就可以表示多个值传递 如果按顺序就放在args 为元组 如果传nid 就放在 kwargs 为字典
    return HttpResponse(kwargs['nid'])

        实战:
            a:  url(r'^detail-(\d+)-(\d+).html', views.detail),
                def func(request, nid, uid):
                    pass

            b:  url(r'^detail-(\d+)-(\d+).html', views.detail),
                def func(request,*args):
                    return HttpResponse(args[1])

            c:  url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html', views.detail),
                def func(request, *args,**kwargs):
                    kwargs = {'nid':1, "uid":2}

    4、 name  对URL路由关系进行命名,****** 以后可以根据此名称生成自己想要的URL *****
            a:  <form action="{% url 'indexx' %}" method="POST">
                url(r'^index/', views.index, name='i1')
            b:  url(r'^cc/(\d+)/(\d+)' , views.index, name='i2'),

            c:  url(r'^dd/(?P<nid>\d+)/(?P<uid>\d+)' , views.index, name='i3'),

            def func(request, *args, **kwargs):
                from django.urls import reverse

                url1 = reverse('i1')  #index/
                url2 = reverse('i2', args=(1, 2,))  #cc/1/2
                url3 = reverse('i3', kwargs={"nid":1,"uid":2,}) dd/1/2

            模板语言
            xxx.html
                {% url "i1" %} #index/
                {% url 'i2' 1 2 %} #cc/1/2
                {% url nid=1 uid=2 %} #dd/1/2

            模板语言:
                {% url "indexx" %}
                {% url "indexx" 2 %}
            注:request.path_info 为当前的url  {{ request.path.info }}

    5、url的分发
    一级路径 主目录下 urls.py
    from django.contrib import admin
    from django.conf.urls import url,include
    urlpatterns = [
        url(r'^cmdb/', include("app01.urls")),
        url(r'^db/', include("app02.urls")),
    ]

    在app01下创建一个urls.py  二级路径
    from django.contrib import admin
    from django.conf.urls import url,include
    from app01 import views

    urlpatterns = [
        url(r'^login/', views.login),
    ]

 

posted @ 2018-06-12 23:15  想翻身的猫  阅读(194)  评论(0编辑  收藏  举报