简体   繁体   English

Django:将URL参数附加到列表中?

[英]Django: append URL parameters to a list?

I currently have URLs of the form /blue - each URL is a colour. 我目前有/blue表格的网址 - 每个网址都是一种颜色。 The associated URL pattern is as follows: 关联的URL模式如下:

 (r'^(?P<colour>\w+)$', 'views.colour')

I'm wondering if it's possible to have URLs that look like a natural language list, of indeterminate length, separated by -or- : 我想知道是否有可能让URL看起来像一个自然语言列表,长度不确定,用-or-分隔:

/blue-or-green-or-yellow

Ideally the associated URL pattern would append each match to a Python list, ready to be handled in the view: 理想情况下,关联的URL模式会将每个匹配附加到Python列表,准备在视图中处理:

 (r'^(?P<colour_list>\w+)(?:-or-(?P<colour_list>\w+))+$', 'views.colour')

Is there any way to do this in Django? 在Django有没有办法做到这一点?

Something like (?P<colour_list>(\\w+(\\-or\\-)?)+) will get the entire substring match, then you can just split by -or- (?P<colour_list>(\\w+(\\-or\\-)?)+)会获得整个子串匹配,然后你可以按以下方式拆分-or-

Note, however, that then blue-or- would be valid match, so you may want to split it like this: filter(bool, colour_list.split('-or-')) 但请注意,然后blue-or-有效匹配,因此您可能希望将其拆分为: filter(bool, colour_list.split('-or-'))

Something like this will help: 这样的东西会有所帮助:

takes comma separated colours 用逗号分隔的颜色

(r'^(?P<colours>[\w,]+)$', 'views.colour')

then in view: 然后在视野中:

colours = colours.split(',')

Try this regex : 试试这个regex

(\w+(?:-or-)?)+

or use string split: 或使用字符串拆分:

result = colours.split("-or-")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM