动态路由和动态参数捕获

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

捕获参数:

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

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

路由分发

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

from django.conf.urls import include, url

urlpatterns=[
url("^app01", include("app01.url")) # 这里写导入路径或者把url导入进来写在这里都可以
]

app01.urls

from django.conf.urls import include, url

urlpatterns=[
url("nihao", func)
]

include -  返回的是个元组

def include(arg, namespace=None, app_name=None):
if app_name and not namespace:
raise ValueError('Must specify a namespace if specifying app_name.')
if app_name:
warnings.warn(
'The app_name argument to django.conf.urls.include() is deprecated. '
'Set the app_name in the included URLconf instead.',
RemovedInDjango20Warning, stacklevel=2
) if isinstance(arg, tuple):
# callable returning a namespace hint
try:
urlconf_module, app_name = arg
except ValueError:
if namespace:
raise ImproperlyConfigured(
'Cannot override the namespace for a dynamic module that provides a namespace'
)
warnings.warn(
'Passing a 3-tuple to django.conf.urls.include() is deprecated. '
'Pass a 2-tuple containing the list of patterns and app_name, '
'and provide the namespace argument to include() instead.',
RemovedInDjango20Warning, stacklevel=2
)
urlconf_module, app_name, namespace = arg
else:
# No namespace hint - use manually provided namespace
urlconf_module = arg if isinstance(urlconf_module, six.string_types): # 字符串类型的就导入进来
urlconf_module = import_module(urlconf_module)
patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
app_name = getattr(urlconf_module, 'app_name', app_name)
if namespace and not app_name:
warnings.warn(
'Specifying a namespace in django.conf.urls.include() without '
'providing an app_name is deprecated. Set the app_name attribute '
'in the included module, or pass a 2-tuple containing the list of '
'patterns and app_name instead.',
RemovedInDjango20Warning, stacklevel=2
) namespace = namespace or app_name # Make sure we can iterate through the patterns (without this, some
# testcases will break).
if isinstance(patterns, (list, tuple)):
for url_pattern in patterns:
# Test if the LocaleRegexURLResolver is used within the include;
# this should throw an error since this is not allowed!
if isinstance(url_pattern, LocaleRegexURLResolver):
raise ImproperlyConfigured(
'Using i18n_patterns in an included URLconf is not allowed.') return (urlconf_module, app_name, namespace) def url(regex, view, kwargs=None, name=None):
if isinstance(view, (list, tuple)):
# For include(...) processing.
urlconf_module, app_name, namespace = view
return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace)
elif callable(view):
return RegexURLPattern(regex, view, kwargs, name)
else:
raise TypeError('view must be a callable or a list/tuple in the case of include().')

反向解析和名称空间

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

反向解析就为此而生

无参路由

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

urlpatterns = [
url("aaaa", func, name="name")
]

在视图中使用

from django.urls import reverse

reverse('name')

模板中使用

{% url "name" %}

有参路由

同上

urlpatterns = [
url("aaaa/(/d+)", func, name="name")
]

在视图中使用

from django.urls import reverse

reverse('name', args=(1,)) # 无名参数, 按位置传
reverse('name', kwargs={"1":1}) # 命名参数, 按关键字传

在路由中使用

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

名称空间

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

urlpatterns=[
url("^app01", include("app01.url", namespace="app01"))
url("^app02", include("app02.url", namespace="app02"))
]

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

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

路由中使用

{% 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. jQuery手机端点击弹出分享按钮代码

    一.HTML代码如下: <span onClick="toshare()" style="border:dotted 1px #ddd;display:block; ...

  2. Cnario Player 接入视频采集卡采集外部音视频信号测试

    测试产品 型号: TC-D56N1-30P采集卡 参数: 1* HDMI 1.4输入, PCIe 接口为PCI-Express x4(Gen2), 最高支持4096x2160@30Hz, 支持1920 ...

  3. Javascript实现base64的加密解密

    //1.加密解密方法使用: //1.加密 var str = '124中文内容'; var base = new Base64(); var result = base.encode(str); // ...

  4. Url校验正则

    最近需要对HTTP请求合法性做一些校验,在网上查找了一些关于URL合法性的正则表达式. 在github上的有个关于weburl匹配的gist: https://gist.github.com/dper ...

  5. 在access转sql server指定的转换无效

    今天继续昨天没解决的问题,还是继续报错数据库中“指定的转换无效”,看着这个错误都有点头大了,啊啊啊啊,因为无法再代码中寻找到具体点,只好在晚上继续查询 最后终于看到一条帖子说在转换的时候可能出现数据冲 ...

  6. PHP RSA加解密详解(附代码)

    前言:RSA加密一般用在涉及到重要数据时所使用的加密算法,比如用户的账户密码传输,订单的相关数据传输等. 加密方式说明:公钥加密,私钥解密.也可以  私钥加密,公钥解密 一.RSA简介 RSA公钥加密 ...

  7. python 内置函数详解

    懒得写了  参考1:https://www.cnblogs.com/xiao1/p/5856890.html 参考2:https://www.runoob.com/python/python-buil ...

  8. Ubuntu几个常用命令

    命令 > file 重定向,清空file文件 命令 >>file 重定向,不清空文件,在尾部追加 英文对照:

  9. URL存在跨站漏洞http host头攻击漏洞解决方案

    最近项目部署的时候客户使用的绿盟扫描出一些漏洞,老大让我处理,经过看大神的博客等方式,分享一些简单的解决方法. 一 跨网站脚本 跨网站脚本(Cross-site scripting,通常简称为XSS或 ...

  10. I/O模型系列之三:IO通信模型BIO NIO AIO

    一.传统的BIO 网络编程的基本模型是Client/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(绑定的IP地址和监听端口),客户端通过连接操作向服务端监听的地址发起连接请 ...