动态路由和动态参数捕获

动态路由:url支持正则表达式, 访问的url能够匹配成功就去执行对应的视图函数

捕获参数:

  1. # 捕获参数,位置传参
  2. url(r'^data/([0-9]{4})/([0-2][0-9])/',function)
  3. # 关键字传参
  4. url(r'^data/(?P<year>[0-9]{4})/(?P<day>[0-2][0-9])/',function)

  视图函数中要给参数预留位置

路由分发

将不同功能的路由进行拆分, 将以某个匹配开头的路由分发到指定包去处理, 继续匹配后面内容

  1. from django.conf.urls import include, url
  2.  
  3. urlpatterns=[
  4. url("^app01", include("app01.url")) # 这里写导入路径或者把url导入进来写在这里都可以
  5. ]

app01.urls

  1. from django.conf.urls import include, url
  2.  
  3. urlpatterns=[
  4. url("nihao", func)
  5. ]

include -  返回的是个元组

  1. def include(arg, namespace=None, app_name=None):
  2. if app_name and not namespace:
  3. raise ValueError('Must specify a namespace if specifying app_name.')
  4. if app_name:
  5. warnings.warn(
  6. 'The app_name argument to django.conf.urls.include() is deprecated. '
  7. 'Set the app_name in the included URLconf instead.',
  8. RemovedInDjango20Warning, stacklevel=2
  9. )
  10.  
  11. if isinstance(arg, tuple):
  12. # callable returning a namespace hint
  13. try:
  14. urlconf_module, app_name = arg
  15. except ValueError:
  16. if namespace:
  17. raise ImproperlyConfigured(
  18. 'Cannot override the namespace for a dynamic module that provides a namespace'
  19. )
  20. warnings.warn(
  21. 'Passing a 3-tuple to django.conf.urls.include() is deprecated. '
  22. 'Pass a 2-tuple containing the list of patterns and app_name, '
  23. 'and provide the namespace argument to include() instead.',
  24. RemovedInDjango20Warning, stacklevel=2
  25. )
  26. urlconf_module, app_name, namespace = arg
  27. else:
  28. # No namespace hint - use manually provided namespace
  29. urlconf_module = arg
  30.  
  31. if isinstance(urlconf_module, six.string_types): # 字符串类型的就导入进来
  32. urlconf_module = import_module(urlconf_module)
  33. patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
  34. app_name = getattr(urlconf_module, 'app_name', app_name)
  35. if namespace and not app_name:
  36. warnings.warn(
  37. 'Specifying a namespace in django.conf.urls.include() without '
  38. 'providing an app_name is deprecated. Set the app_name attribute '
  39. 'in the included module, or pass a 2-tuple containing the list of '
  40. 'patterns and app_name instead.',
  41. RemovedInDjango20Warning, stacklevel=2
  42. )
  43.  
  44. namespace = namespace or app_name
  45.  
  46. # Make sure we can iterate through the patterns (without this, some
  47. # testcases will break).
  48. if isinstance(patterns, (list, tuple)):
  49. for url_pattern in patterns:
  50. # Test if the LocaleRegexURLResolver is used within the include;
  51. # this should throw an error since this is not allowed!
  52. if isinstance(url_pattern, LocaleRegexURLResolver):
  53. raise ImproperlyConfigured(
  54. 'Using i18n_patterns in an included URLconf is not allowed.')
  55.  
  56. return (urlconf_module, app_name, namespace)
  57.  
  58. def url(regex, view, kwargs=None, name=None):
  59. if isinstance(view, (list, tuple)):
  60. # For include(...) processing.
  61. urlconf_module, app_name, namespace = view
  62. return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace)
  63. elif callable(view):
  64. return RegexURLPattern(regex, view, kwargs, name)
  65. else:
  66. raise TypeError('view must be a callable or a list/tuple in the case of include().')

反向解析和名称空间

反向解析的应用场景 :  在试图函数和模板中写了很多跳转的路由, 然后领导要求把urlpatterns里面改了,,, 改了这里相关的试图和html中的也要改,,,很容易漏掉

反向解析就为此而生

无参路由

首先要为路由设置一个名字

  1. urlpatterns = [
  2. url("aaaa", func, name="name")
  3. ]

在视图中使用

  1. from django.urls import reverse
  2.  
  3. reverse('name')

模板中使用

  1. {% url "name" %}

有参路由

同上

  1. urlpatterns = [
  2. url("aaaa/(/d+)", func, name="name")
  3. ]

在视图中使用

  1. from django.urls import reverse
  2.  
  3. reverse('name', args=(1,)) # 无名参数, 按位置传
  4. reverse('name', kwargs={"1":1}) # 命名参数, 按关键字传

在路由中使用

  1. {% url "name" 1 %} # 按位置传
  2. {% url "name" q1="1" %} # 按关键字传

名称空间

对于include其中的路由出现的同名现象, 不同功能中的路由可能是不同的人写的, 对于出现的同名的路由, 前面的那个会被替换.

  1. urlpatterns=[
  2. url("^app01", include("app01.url", namespace="app01"))
  3. url("^app02", include("app02.url", namespace="app02"))
  4. ]

当设置了namespace时, 反向解析时必须用到

  1. from django.urls import reverse
  2. reverse('app01:name')
  3. # 带参数的同上

路由中使用

  1. {% url "app01:name" %}

  

Django_路由详的更多相关文章

  1. Ocelot简易教程(三)之主要特性及路由详解

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9664977.html 上篇<Ocelot简易教程(二)之快速开始2>教大家如何快速跑起来一个 ...

  2. Vue 路由详解

    Vue 路由详解 对于前端来说,其实浏览器配合超级连接就很好的实现了路由功能.但是对于单页面应用来说,浏览器和超级连接的跳转方式已经不能适用,所以各大框架纷纷给出了单页面应用的解决路由跳转的方案. V ...

  3. elasticsearch系列三:索引详解(分词器、文档管理、路由详解(集群))

    一.分词器 1. 认识分词器  1.1 Analyzer   分析器 在ES中一个Analyzer 由下面三种组件组合而成: character filter :字符过滤器,对文本进行字符过滤处理,如 ...

  4. Express的路由详解

    Express的路由详解 http://www.jb51.net/article/76203.htm

  5. [转载]Ocelot简易教程(三)之主要特性及路由详解

    上篇<Ocelot简易教程(二)之快速开始2>教大家如何快速跑起来一个ocelot实例项目,也只是简单的对Ocelot进行了配置,这篇文章会给大家详细的介绍一下Ocelot的配置信息.希望 ...

  6. Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解

    如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 最近一段时间生病了,整天往医院跑,也没状态学东西了,现在是好了不少了,也该继续学习啦!!! ...

  7. iOS路由详解

    本文如题,路由详解,注定是一篇详细解释iOS路由原理及使用的文章,由于此时正在外地出差,无法详细一一写出,只能不定时的补充. 一.什么是iOS路由 路由一词来源于路由器,可以实现层级之间消息转发的功能 ...

  8. vue技术栈进阶(02.路由详解—基础)

    路由详解(一)--基础: 1)router-link和router-view组件 2)路由配置 3)JS操作路由

  9. Angular6 学习笔记——路由详解

    angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...

随机推荐

  1. Unity TimeLine

    最近一直再看这方面的内容,看的比较多知识点比较分散,所以目的就是把这些知识点内容梳理一边,并作记录. PlayableDirector与TrackAsset,TrackAsset与PlayableAs ...

  2. UnityInspector显示扩展

    比如经常在三方插件中看到如下在Inspector中的样式 这种对特别是要做编辑序列化数据脚本操作很友好,但是这个是如何实现呢?比如我们要创建一个保存序列化的npc基本数据,名字(Name),性别(Se ...

  3. Springboot+mybatis中整合过程访问Mysql数据库时报错

    报错原因如下:com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone.. 产生这个 ...

  4. 微信支付之02------整个微信支付功能----------Java实现

    先来看下微信支付官方文档: 1.在官方文档上有很多种支付方式,由于目前我只做过JSAPI和微信扫码支付二种,其他的就不说了. >>>>>第一种微信扫码支付>> ...

  5. Windows kafka_2.11-1.1.0安装

    #broker.id= #broker.list=,, listeners=PLAINTEXT://127.0.0.1:9092 advertised.listeners=PLAINTEXT://12 ...

  6. CodeForces 1151E Number of Components

    题目链接:http://codeforces.com/problemset/problem/1151/E 题目大意: n个人排成一个序列,标号为 1~n,第 i 个人的学习成绩为 ai,现在要选出学习 ...

  7. c提高第四课

    1.一维数组的初始化 , , }; //3个元素 ] = { , , }; //a[3], a[4]自动初始化为0 ] = { }; //全部元素初始化为0 memset(c, , sizeof(c) ...

  8. css居中flex

    css利用flex实现居中(子元素可以不必管宽高):

  9. 一、Log4Net配置

    Core的配置 一.创建core包含控制和视图的项目以及Log4Net引用 二.创建Log4Net配置文件 右击项目->添加文件   Log4Net.config 2 复制以下代码 以下配置可做 ...

  10. 简单 php 代码跟踪调试实现

    简单 php 代码跟踪调试实现 debug_backtrace:生成回溯 debug_print_backtrace:打印回溯 1. debug_backtrace ($options = DEBUG ...