0

We have a blog created with Django 4. And have articles where for each article exists a url like this

www.example.com/id/slug

but for some of them we changed the id and we want to redirect the old urls to the new one. For example we want to turn

www.example.com/2/slug1

to

www.example.com/56/slug1

And this is the urls.py content:

urlpatterns = [
    path('<pk>/<custom_slug:persian_slug>/', 
        views.post_detail, name='post_detail'),
]

And here is views.py content:


def post_detail(request, pk, persian_slug):
    template_file = 'posts/post/post.html'
    post = get_object_or_404(Magazine, id=pk)
    latest_posts = Magazine.objects.filter(enable=1).filter(service_id__in=[29, 48, 58, 78]).\
        order_by('-created').using('default')[:3]

    subject = ''
    name = ''
    service_id = post.service_id

    context = {'post': post, 'latest_posts': latest_posts, 'subject': subject, 'name': name}

    return render(request, template_file, context)

How can we do this in Django 4?

sam
  • 1
  • 2

0 Answers0