[Answer]-Redirect URL with Argument in urls.py in Django 1.6.5

1👍

✅

The view_blog_post pattern requires a slug, so you can’t just do reverse_lazy('view_blog_post') because it does not use the slug.

Instead, you can use the pattern_name argument, then Django will use the args and kwargs to reverse the url.

You also want to make the year and month non capturing groups using ?:, because you do not want to use these to reverse the url.

url(r'^(?:\d{4})/(?:\d{1,2})/(?P<slug>[^\.]+)', RedirectView.as_view(pattern_name='view_blog_post', permanent=True)),

The default of permanent is switching to False in Django 1.9, so it’s good to set it explicitly to prevent it from changing accidentally when you upgrade.

Leave a comment