0

urls with 2 request params:

/prefix1/1/
/prefix2/2/
/prefix1/1/prefix2/2
/prefix2/2/prefix1/1

url( ur'^prefix1/(?P<p1>\d+)/$', 'app.views.view' ),
url( ur'^prefix2/(?P<p2>\d+)/$', 'app.views.view' ),
url( ur'^prefix1/(?P<p1>\d+)/prefix2/(?P<p2>\d+)/$', 'app.views.view' ),
url( ur'^prefix2/(?P<p2>\d+)/prefix1/(?P<p1>\d+)/$', 'app.views.view' ),

Is possible to do this more 'DRY' (with 3 request params, lines in urls.py = 15) ?

cetver
  • 11,279
  • 5
  • 36
  • 56

1 Answers1

0

I hacked together a sample and, if lazerscience's comment doesn't do it for you, here's what I came up with:

url(r'^(?P<param1>foo|bar)(/(?P<param2>\d+))?(/(?P<param3>\d+))?(/(?P<param4>\d+))?/$', 'demo.views.view'),

And my view looked like:

def view(request, *args, **kwargs):
    return render_to_response("index.html",
        { 'dict': [(k, v) for k,v in kwargs.iteritems()] },
        context_instance=RequestContext(request))

It progressively added parameters as you added to the url, left to right, just as you'd expect.

Elf Sternberg
  • 16,129
  • 6
  • 60
  • 68
  • not exactly that i nee, you forgot about `prefix`, see http://stackoverflow.com/questions/5399035/django-regex-for-optional-url-paramaters/5399101#5399101 – cetver Sep 24 '11 at 15:14
  • No, I didn't. I assumed you knew enough to be able to derive an equivalent prefix from the `foo|bar` example expression above. – Elf Sternberg Sep 25 '11 at 23:32