slug in redirect django
First, you need to import the redirect and get_object_or_404 functions:
from django.shortcuts import redirect, get_object_or_404
Then, in your view function, use the get_object_or_404 function to retrieve the object based on the slug:
from django.urls import reverse
from django.http import Http404
def my_view(request, slug):
my_object = get_object_or_404(MyModel, slug=slug)
After that, construct the URL using the reverse function and the object's detail view name:
url = reverse('detail-view-name', args=[my_object.id])
Finally, use the redirect function to redirect to the constructed URL:
return redirect(url)