Search code examples
pythondjangoparametersurlconf

Multiple parameters in Django URLconf


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?


Solution

  • 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.