开发者

Multiple parameters in Django URLconf

开发者 https://www.devze.com 2023-03-07 07:37 出处:网络
Say 开发者_运维问答I want my URLs to have alphanumeric string parameters, where each parameter is separated by a \'+\' and we have more than 1 parameter. That is, blah.com/1a+2b would be valid, but bl

Say 开发者_运维问答I want my URLs to have alphanumeric string parameters, where each parameter is separated by a '+' and we have more than 1 parameter. That is, blah.com/1a+2b would be valid, but blah.com/1a or blah.com/1a_2b would not be valid.

So far, in my urls.py, I have:

(r'^((\w+)\+)+(\w+)$', 'XXX.views.YYY')

and in views.py:

YYY(request, name, args*)

Anyone know how to go about doing this?


Take the string in your URLConf and validate in your view.

url(r'^(?P<params_list>(\w+)\+?)+)/$');

In your view:

def my_view(request, params):
  if not validate_params(params): # validate as you wish
    raise Http404()

  # continue with your view

Or, if your view already expects a list, you could write a decorator to take the string, parse it and pass it to your view as a list.

0

精彩评论

暂无评论...
验证码 换一张
取 消