Django:任意数量的未命名的urls.py参数 [英] Django: Arbitrary number of unnamed urls.py parameters

查看:147
本文介绍了Django:任意数量的未命名的urls.py参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有大量字段和20000+表行的Django模型。为了方便人类可读的URL和将大型列表分解成任意子列表的功能,我想要一个如下所示的URL:

  / browse /< name1> /< value1> /< name2> /< value2> / ....等.... 

其中'name'映射到模型属性,value是该属性的搜索条件。每个名称将被视为类别,以返回类别匹配的模型实例的子集。



现在,这可以使用GET参数来处理,但我更喜欢为用户和搜索引擎提供更可读的URL。这些网址子集将嵌入显示此模型的每个页面上,所以似乎值得努力制作漂亮的网址。



理想情况下,每个名称/值对都将传递给视图函数作为参数名为 name1 name2 等。但是,我不相信可以定义通过正则表达式的匹配文本命名模式。我错了吗?



所以,似乎我需要这样做:

  urlpatterns = patterns('',
url(r'^ browse /(?:([\w] +)/([\w] +)/)+ $' 'app.views.view',name =model_browse),

它似乎这应该匹配任何一组两个名称/值对。虽然它成功匹配,但它只将最后一个名称/值对作为参数传递给视图函数。我的猜测是,每场比赛都是覆盖以前的比赛。根据含有(?:...)+的猜测,我尝试了一个简单的重复模式:

  urlpatterns = patterns('',
url(r'^ browse /([\w] + /)+ $','app.views.view',name =model_browse),

...也有同样的问题,但这次 * args 仅包含最后匹配的模式。



这是Django的url调度程序和/或Python的正则表达式支持的限制?似乎这些方法中的任何一种都应该起作用。有没有办法实现这一点,而不是将URL中的每个可能的模型属性硬编码为可选的(。*)模式?

解决方案

您可能会考虑的可能性是匹配url模式部分中的整个可能值字符串,并在视图中拉出特定的片段。例如:

  urlpatterns = patterns('',
url(r'^ browse /(?匹配>。+)/ $','app.views.view',name ='model_browse'),


def视图(请求,匹配):
件= match.split('/')
#甚至索引的作品是名字,奇数是
...

没有对我使用的正则表达式的承诺,但我认为你明白我的意思。



(编辑以修复正则表达式。)


I have a Django model with a large number of fields and 20000+ table rows. To facilitate human readable URLs and the ability to break down the large list into arbitrary sublists, I would like to have a URL that looks like this:

/browse/<name1>/<value1>/<name2>/<value2>/ .... etc ....

where 'name' maps to a model attribute and 'value' is the search criteria for that attribute. Each "name" will be treated like a category to return subsets of the model instances where the categories match.

Now, this could be handled with GET parameters, but I prefer more readable URLs for both the user's sake and the search engines. These URLs subsets will be embedded on each page that displays this model, so it seems worth the effort to make pretty URLs.

Ideally each name/value pair will be passed to the view function as a parameter named name1, name2, etc. However, I don't believe it's possible to defined named patterns via a regex's matched text. Am I wrong there?

So, it seems I need to do something like this:

urlpatterns = patterns('',
    url(r'^browse/(?:([\w]+)/([\w]+)/)+$', 'app.views.view', name="model_browse"),
)

It seems this should match any sets of two name/value pairs. While it matches it successfully, it only passes the last name/value pair as parameters to the view function. My guess is that each match is overwriting the previous match. Under the guess that the containing (?:...)+ is causing it, I tried a simple repeating pattern instead:

urlpatterns = patterns('',
    url(r'^browse/([\w]+/)+$', 'app.views.view', name="model_browse"),
)

... and got the same problem, but this time *args only includes the last matched pattern.

Is this a limitation of Django's url dispatcher, and/or Python's regex support? It seems either of these methods should work. Is there a way to achieve this without hardcoding each possible model attribute in the URL as an optional (.*) pattern?

解决方案

A possibility that you might consider is matching the entire string of possible values within the url pattern portion and pull out the specific pieces within your view. As an example:

urlpatterns = patterns('',
    url(r'^browse/(?P<match>.+)/$', 'app.views.view', name='model_browse'),
)

def view(request, match):
    pieces = match.split('/')
    # even indexed pieces are the names, odd are values
    ...

No promises about the regexp I used, but I think you understand what I mean.

(Edited to try and fix the regexp.)

这篇关于Django:任意数量的未命名的urls.py参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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