使用正则表达式发送多个URL [英] Multiple url dispatches with Regex

查看:137
本文介绍了使用正则表达式发送多个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个URL发送。一个在 http://domain.com/thisword 上捕获单词,而第二个调度是 http://domain.com/上的站点地图sitemap.xml的。当前代码不正确的是:

  urlpatterns = patterns('',
url(ur' ?P< search_word> [ÆØÅæøåa-zA-Z] *)/?$','website.views.index_view',name ='website_index'),
url(r'^ sitemap\.xml $' ,'django.contrib.sitemaps.views.sitemap',{'sitemaps':sitemaps}),

所以基本上第一个dispatch捕获所有东西,包括 sitemap.xml 。是否可以按照以下方式进行多次调度?

解决方案

好的问题。 (感谢您在这里发布完整的代码,现在我看看你以后是什么,我想)最简单的解决方案是扭转这样的模式:

  urlpatterns = patterns('',
url(r'^ sitemap\.xml $','django.contrib.sitemaps.views.sitemap',{'sitemaps':sitemaps} ),
url(ur'(?P< search_word> [ÆØÅæøåa-zA-Z] *)/?$','website.views.index_view',name ='website_index'),

调度员在找到匹配的那一刻发送。因此,如果一个网址匹配上述 urlpatterns 中的 r'^ sitemap\.xml $ ,调度员将不会继续到第二个模式


I have a two URL dispatches. One that catches words on http://domain.com/thisword, while the second dispatch is a sitemap on http://domain.com/sitemap.xml. The current code which does not work correct is:

urlpatterns = patterns('',
    url(ur'(?P<search_word>[ÆØÅæøåa-zA-Z]*)/?$', 'website.views.index_view', name='website_index'),
    url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
)

So basically the first dispatch catches everything, including sitemap.xml. Is it possible to have multiple dispatches in following fashion?

解决方案

Good question. (Thanks for posting the full code here. Now I see what you are after, I think.) The easiest solution would be to reverse the patterns like this:

urlpatterns = patterns('',
    url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
    url(ur'(?P<search_word>[ÆØÅæøåa-zA-Z]*)/?$', 'website.views.index_view', name='website_index'),
)

The dispatcher dispatches the moment it finds a match. So if a url matches r'^sitemap\.xml$ in the urlpatterns above, the dispatcher will not continue to the second pattern

这篇关于使用正则表达式发送多个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆