django的url匹配

学习网站:官方

以下是个人摘录:

1.基本匹配

urlpatterns = patterns(‘‘,
    url(r‘^articles/2003/$‘, ‘news.views.special_case_2003‘),
    url(r‘^articles/(\d{4})/$‘, ‘news.views.year_archive‘),
    url(r‘^articles/(\d{4})/(\d{2})/$‘, ‘news.views.month_archive‘),
    url(r‘^articles/(\d{4})/(\d{2})/(\d+)/$‘, ‘news.views.article_detail‘),
)

/articles/2005/03/ 这个匹配第三个url,即news.views.mouth_archive(2005, 03)

/articles/2003/03/03/ 匹配第4个,即 news.views.article_detail(request, ‘2003‘, ‘03‘, ‘03‘).

2.关键字匹配

urlpatterns = patterns(‘‘,
    url(r‘^articles/2003/$‘, ‘news.views.special_case_2003‘),
    url(r‘^articles/(?P<year>\d{4})/$‘, ‘news.views.year_archive‘),
    url(r‘^articles/(?P<year>\d{4})/(?P<month>\d{2})/$‘, ‘news.views.month_archive‘),
    url(r‘^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$‘, ‘news.views.article_detail‘),
)

/articles/2005/03/news.views.month_archive(request, year=‘2005‘, month=‘03‘),代替了news.views.month_archive(request, ‘2005‘, ‘03‘).

这里是不是可以把url各个参数都获取出来呢?当然

3.默认值匹配

urlpatterns = patterns(‘‘,
    url(r‘^blog/$‘, ‘blog.views.page‘),
    url(r‘^blog/page(?P<num>\d+)/$‘, ‘blog.views.page‘),
)

# View (in blog/views.py)
def page(request, num="1"):
    # Output the appropriate page of blog entries, according to num.
    ...

如果是第一个匹配到了,那么num会默认为1

4.模型

假如:

urlpatterns = patterns(‘‘,
    url(r‘^articles/(\d{4})/$‘, ‘news.views.year_archive‘),
    url(r‘^articles/(\d{4})/(\d{2})/$‘, ‘news.views.month_archive‘),
    url(r‘^articles/(\d{4})/(\d{2})/(\d+)/$‘, ‘news.views.article_detail‘),
)

则可代替为:

urlpatterns = patterns(‘news.views‘,
    url(r‘^articles/(\d{4})/$‘, ‘year_archive‘),
    url(r‘^articles/(\d{4})/(\d{2})/$‘, ‘month_archive‘),
    url(r‘^articles/(\d{4})/(\d{2})/(\d+)/$‘, ‘article_detail‘),
)

注意patterns第一个参数变化

5.多模型

假如:

urlpatterns = patterns(‘‘,
    url(r‘^$‘, ‘myapp.views.app_index‘),
    url(r‘^(?P<year>\d{4})/(?P<month>[a-z]{3})/$‘, ‘myapp.views.month_display‘),
    url(r‘^tag/(?P<tag>\w+)/$‘, ‘weblog.views.tag‘),
)

则可代替为:

urlpatterns = patterns(‘myapp.views‘,
    url(r‘^$‘, ‘app_index‘),
    url(r‘^(?P<year>\d{4})/(?P<month>[a-z]{3})/$‘,‘month_display‘),
)

urlpatterns += patterns(‘weblog.views‘,
    url(r‘^tag/(?P<tag>\w+)/$‘, ‘tag‘),
)

6.include

urlpatterns = patterns(‘‘,
    # ... snip ...
    url(r‘^comments/‘, include(‘django.contrib.comments.urls‘)),
    url(r‘^community/‘, include(‘django_website.aggregator.urls‘)),
    url(r‘^contact/‘, include(‘django_website.contact.urls‘)),
    # ... snip ...
)

下面这个用法也可以:

extra_patterns = patterns(‘‘,
    url(r‘^reports/(?P<id>\d+)/$‘, ‘credit.views.report‘),
    url(r‘^charge/$‘, ‘credit.views.charge‘),
)

urlpatterns = patterns(‘‘,
    url(r‘^$‘, ‘apps.main.views.homepage‘),
    url(r‘^help/‘, include(‘apps.help.urls‘)),
    url(r‘^credit/‘, include(extra_patterns)),
)

下面这个是不是很复杂?

urlpatterns = patterns(‘wiki.views‘,
    url(r‘^(?P<page_slug>\w+)-(?P<page_id>\w+)/history/$‘, ‘history‘),
    url(r‘^(?P<page_slug>\w+)-(?P<page_id>\w+)/edit/$‘, ‘edit‘),
    url(r‘^(?P<page_slug>\w+)-(?P<page_id>\w+)/discuss/$‘, ‘discuss‘),
    url(r‘^(?P<page_slug>\w+)-(?P<page_id>\w+)/permissions/$‘, ‘permissions‘),
)

在替换下吧:

urlpatterns = patterns(‘‘,
    url(r‘^(?P<page_slug>\w+)-(?P<page_id>\w+)/‘, include(patterns(‘wiki.views‘,
        url(r‘^history/$‘, ‘history‘),
        url(r‘^edit/$‘, ‘edit‘),
        url(r‘^discuss/$‘, ‘discuss‘),
        url(r‘^permissions/$‘, ‘permissions‘),
    ))),
)

7.额外参数

urlpatterns = patterns(‘blog.views‘,
    url(r‘^blog/(?P<year>\d{4})/$‘, ‘year_archive‘, {‘foo‘: ‘bar‘}),
)

/blog/2005/,即blog.views.year_archive(request, year=‘2005‘, foo=‘bar‘).

时间: 2024-10-09 14:24:16

django的url匹配的相关文章

Django中url使用命名空间的错误

出的错误: 1. Reverse for 'llist' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] 2. Reverse for 'home' with arguments '(1L,)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'org/home/?P<org_id>(/d+)/$'] 出错原因

django中“url映射规则”和“服务端响应顺序”

1.django搜索路径 使用 import 语句时,Python 所查找的系统目录清单. 查看方式: import sys print sys.path 通常无需关心 Python 搜索路径的设置,Python 和 Django 会在后台自动帮你处理好. 2.url匹配模式  基本结构: '^需要匹配的url字符串$' PS:实际上最终完整的url串是http://根路径:端口号/需要匹配的url字符串 系统自动添加的部分'http://根路径:端口号/' eg:url匹配模式:'^lates

Django的url用法

利用Django开发网站,可以设计出非常优美的url规则,如果url的匹配规则(包含正则表达式)组织得比较好,view的结构就会比较清晰,比较容易维护. 最简单的形式 from django.conf.urls import patterns, url urlpatterns = patterns('', url(r'^articles/2003/$', 'news.views.special_case_2003'), url(r'^articles/(\d{4})/$', 'news.view

Python自动化运维:Django之URL路由

准备首先新建一个Django 项目 django-admin startproject urlTest 进入manage.py所在目录后 ./manage.py startapp app1 ./manage.py startpap app2 此时我们新建了一个名为urlTest的项目,其中有两个模块的名称分别为app1和app2.(树目录结构如下) . ├── app1 │   ├── __init__.py │   ├── admin.py │   ├── apps.py │   ├── mi

Django之url上的include,URL命名和反向解析,命名空间 以及图书管理系统删除功能二合一方法

include其他的URLconfs #At any point, your urlpatterns can "include" other URLconf modules. This #essentially "roots" a set of URLs below other ones. #For example, here's an excerpt of the URLconf for the Django website itself. #It include

Django路由(URL)

URL配置(URLconf)就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表. 我们就是以这种方式告诉Django,遇到哪个URL的时候,要对应执行哪个函数. URLconf配置 基本格式: from django.conf.urls import url urlpatterns = [ url(正则表达式, views视图,参数,别名), ] 示例: from django.conf.urls import url from . import view

《二》django之url、视图、日志功能logging

django之url 参考文档:https://yiyibooks.cn/xx/Django_1.11.6/topics/http/urls.html 在setting中ROOT_URLCONF = 'devops.urls' 配置 Django如何处理请求 匹配url,都是从第一个往下匹配,若第一个可以匹配,就不用跳到第二个url规则 from django.conf.urls import url,include from . import views app_name = 'dashboa

Django之url路由

Django之url路由 url路由介绍 url路由是关联url及其视图函数关系的过程. 作用就是使views里面处理数据的函数与请求的url建立映射关系 url路由配置 Django的url路由配置在settings.py文件中ROOT_URLCONF变量指定全局路由文件名称. 注意:默认创建项目的时候,就会配置好,如果想使用别的配置文件自行修改. Django的URL路由流程 Django查找全局urlpatterns变量(urls.py) 按照先后顺序,对URL逐一匹配urlpattern

Django之URL路由系统

一 URL配置 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表.你就是以这种方式告诉Django,对于这个URL调用这段代码,对于那个URL调用那段代码. 基本格式  1 2 3 4 5 from django.conf.urls import url #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数执行,就不再往下循环了,并给函数传一个参