• 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反解

  • 相关阅读:
    linux安装python串口工具pyserial遇到不能成功导入的问题
    yum rpm apt-get wget 辨析
    系统时间不一致导致memcached的session不共享
    ERROR: transport error 202: bind failed: Address already in use
    The user specified as a definer ('root'@'%') does not exist
    IE比Chrome强的一个地方
    电脑突然死机,系统日志记录事件ID=6008
    mysql 查询多个id
    tomcat登陆WEB显示无权限问题&& tomcat无限循环启动问题
    MySql中把一个表的数据插入到另一个表中的实现代码
  • 原文地址:https://www.cnblogs.com/zy6103/p/7551348.html
Copyright © 2020-2023  润新知