Possible redirect loop with Unicode product slugs in ProductDetailView
Hello,
I would like to report a possible issue with Unicode slugs in ProductDetailView.redirect_if_necessary().
Environment
- Python: 3.11.15
- Django: 5.1.3 (also tested with Django 4.2.30)
- django-oscar: 4.1
Problem
When a product has a Unicode slug (for example Persian characters), the product detail page enters a redirect loop.
Example product slug:
گوشی-موبایل-موتورولا-مدل-edge-60-fusion
The generated URL from get_absolute_url() is already percent encoded:
/catalogue/%DA%AF%D9%88%D8%B4%DB%8C-%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84_13/
However, ProductDetailView.redirect_if_necessary() currently compares:
expected_path = product.get_absolute_url()
if quote(expected_path) != quote(current_path):
return HttpResponsePermanentRedirect(expected_path)Since expected_path is already encoded, applying quote() again causes double encoding:
Before:
%DA%AF...
After:
%25DA%25AF...
The browser request path contains the Unicode representation:
/catalogue/گوشی-موبایل_13/
so the comparison never matches and the view keeps returning a permanent redirect.
Debug output
Example:
request.path:
/catalogue/گوشی-موبایل_13/
product.get_absolute_url():
/catalogue/%DA%AF%D9%88%D8%B4%DB%8C-%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84_13/Comparison:
quote(expected_path):
/catalogue/%25DA%25AF...
quote(current_path):
/catalogue/%DA%AF...Workaround
I solved this locally by overriding the method in my Oscar catalogue fork and comparing decoded paths instead:
from urllib.parse import unquote
def redirect_if_necessary(self, current_path, product):
if self.enforce_parent and product.is_child:
return HttpResponsePermanentRedirect(product.parent.get_absolute_url())
if self.enforce_paths:
expected_path = unquote(product.get_absolute_url())
if expected_path.rstrip("/") != current_path.rstrip("/"):
return HttpResponsePermanentRedirect(product.get_absolute_url())This makes both sides Unicode strings before comparison.
Question
Is this expected behavior for Unicode slugs, or should redirect_if_necessary() avoid quoting expected_path because get_absolute_url() already returns a URI-safe encoded path?
Thank you for your work on django-oscar.
Source: django-oscar/django-oscar