Given two categories of entities, I'm selecting some kind of cross product set of them with the following URL definition:
url(r"^category1/(?P<category1>([0123456789]+,?)+)/category2(?P<category2>([0123456789]+,?)+)/$", view, {}, name="cross")
so basically URLs like /category1/1,2,3,4/category2/5,6,7,8/
are valid.
Now I introduced several views onto the same data, so that now I have URLs like /category1/1,2,3,4/category2/5,6,7,8/view1/
and /category1/1,2,3,4/category2/5,6,7,8/view2/
. I would like to redirect the "old" URLs to view1
. I haven't found anything easier than this:
url(r"^category1/(?P<category1>([0123456789]+,?)+)/category2(?P<category2>([0123456789]+,?)+)/$",
redirect_to, {
'url': lazy(lambda: reverse(
'cross_view1',
kwargs={
'category1': '111111',
'category2': '222222',
}
).replace('111111', '%(category1)s') \
.replace('222222', '%(category2)s'), str)(),
name="cross"}
The point here is that I want to reuse my matched groups in the URL, however, I cannot give them as kwargs to redirect_to
, since they wouldn't be interpolated, and neither can I put verbatim formatting in the URL since it has to match my regex (comma-separated numeric IDs). So I introduce some unique ID (111111 and 222222 in this case) and replace them afterwards.
Obviously this feels, looks, smells and tastes very hacky. Is there any cleaner way to do this, apart from introducing an additional view and skipping redirect_to
altogether?