• Django学习之


    路由系统:URL
    1:一个URL对应一个类或函数:
      url(r'^register',reg.register) 函数写法
      url(r'^cbv',reg.cbv.as_view()) 类写法
    2:通过正则实现多个url对应一个类或函数:
      url(r'^detail-(d+).html',reg.detail) 正则匹配detail-任意数字
      url(r'^detail-(?P<nid>d+)-(?P<uid>d+).html',reg.detail) 正则匹配指定关键字(nid,uid)
      另:以上2种情况在函数内可以写成:
      def detail(request,*args,**kwargs):
        pass
      这里:第1个的url的d会传入*args,第2个的url会传入**kwargs
    举例:
      a.
        url(r'^detail-(d+)-(d+).html',reg.detail),
        def func(request,nid,uid): pass
        def func(request,*args): pass
      b.
        url(r'^detail-(?P<nid>d+)-(?P<uid>d+).html',reg.detail),
        def func(request,nid,uid): pass
        def func(request,**kwargs): pass
    4:name
      对URL路由关系进行命名,以后可以根据此名称生成自己想要的URL
      如URL:url(r'^abaaasdfasdfa/',views.home,name='home1'),
      如URL:url(r'^abc/(d+)/(d+)/',views.home,name='home2'),
      如URL:url(r'^def/?P<nid>(d+)/?P<uid>(d+)/',views.home,name='home3'),
      def func(request,*args,**kwargs):
      from django.urls import reverse
        url1 = reverse('home1') # url相当于abaaasdfasdfa/
        url2 = reverse('home2',args=(1,2,)) # url相当于abc/1/2/
        url3 = reverse('home3',kwargs={'nid':1,'uid':2}) # url相当于def/1/2/
      xxx.html
        {% url "home1" %} # abaaasdfasdfa/
        {% url "home2" 1 2 %} # abc/1/2/
        {% url "home3" nid=1 uid=2 %} # def/1/2/
      模板语言:
        {% url "home" %}
        {% url "home" 3 %}
    注:
    # 获取当前的url
      request.path_info
    5.url 路由分发,project下的url为1级(最高级),app里的为二级
    如:
      一级:
      url(r'^cmdb/',include('cmdb.urls')),
      url(r'^app/',include('app01.urls')),
    6.默认值
    参考:http://www.cnblogs.com/wupeiqi/articles/5237704.html
      url(r'^index/$',views.index,{'name':'root'}),

      def index(request,name):
        print(name)
        return HttpResponse('OK')
    7:命名空间:
    一级:
      /admin/ include('app01.urls',namespace='m1')
      /crm/ include('app01.urls',namespace='m2')

    二级:
      app_name='app01'
      url(r'^index/',views.index,name='n1')
      view: v = reverse('m1:n1')
      html: {% url 'm1:n1'%} # 进行url反解

  • 相关阅读:
    2017-09-13
    JavaSE07——异常
    FastDFS入门、搭建以及应用(转载)
    Centos7安装JDK1.8
    「扫盲」 Elasticsearch(转载)
    Java06——面向对象
    Java05——数组
    Java02——基础
    spring boot 配置文件配置项 数字特殊处理问题
    java动态代理机制之自定义实现动态代理
  • 原文地址:https://www.cnblogs.com/zy6103/p/7551348.html
Copyright © 2020-2023  润新知