1

there is a problem when django uses Arabic slugs . It can accepts them . But when you go for its url . It can't find a matching query in database for them . It gives me 404 .

this is the urls.py and my url :

from django.urls import path , re_path
from django.contrib.sitemaps import GenericSitemap 
from .models import Course
from django.contrib.sitemaps.views import sitemap 
from .views import *

app_name = 'course'

info_dict = {
    'queryset': Course.objects.all(),
}

urlpatterns = [
    re_path(r'detail/(?P<slug>[\w_-]+)/$' , detail_course , name='detail_courses'),
    path('sitemap.xml', sitemap, {'sitemaps': {'blog': GenericSitemap(info_dict, priority=0.6)}}, name='django.contrib.sitemaps.views.sitemap'),
]

and its the url that i try to enter :

http://127.0.0.1:8000/course/detail/%D8%AA%D8%AD%D9%84%DB%8C%D9%84_%D8%A8%DB%8C%D8%AA_%DA%A9%D9%88%DB%8C%D9%86/

root urls.py :

from django.contrib import admin
from django.urls import path , include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/' , include('accounts.urls')),
    path('course/' , include('courses.urls')),
    path('orders/' , include('order.urls')),
    path('' , include('home.urls')),
]

what is its problem ?

  • Isn't the problem the underscore? What if you use `[\w_-]` instead? That being said, an underscore normally is *not* included in a slug. – Willem Van Onsem Nov 30 '22 at 13:26
  • No . still giving the same error – Alireza javanpour Nov 30 '22 at 13:31
  • I decoded it with https://www.url-encode-decode.com/ to `تحلیل_بیت_کوین`, and that matches with `[\w_-]` as regex. – Willem Van Onsem Nov 30 '22 at 13:33
  • Escaped URL also needs `%` in allowed symbols but that will not match any slug in your database I assume. And as said, that is not a _slug_ for sure. [Slug](https://stackoverflow.com/questions/427102/what-is-a-slug-in-django) is supposed to avoid such problems of encoding/decoding urls, fighting for good enough regex, showing users unreadable encoded URLs. – Ivan Starostin Nov 30 '22 at 13:39
  • @IvanStarostin: normally the user will not see the percentage encoding, but the unicode counterpart. The percentage encoding is only used as an "envelope" to transfer data in a limited characterset. – Willem Van Onsem Nov 30 '22 at 13:42
  • @Alirezajavanpour: can you please provide the *full* error trace? Normally it will also include the list of patterns. – Willem Van Onsem Nov 30 '22 at 14:05

1 Answers1

0

The problem is not the Arabic characters, but the underscore. You can include it with:

re_path(r'detail/(?P<slug>[\w_-]+)/$', detail_course, name='detail_courses')

That being said, normally a slug does not contain an underscore, so your slugging algorithm does not seem to work properly.

You can use Django's slugify(…) function [Django-doc] for this:

print(slugify(u'تحلیل_بیت_کوین', allow_unicode=True))
تحلیل_بیت_کوین
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • I edited my slug to بیت-کوین and now it does not contain underscores . But i have the same error . – Alireza javanpour Nov 30 '22 at 13:33
  • @Alirezajavanpour: I've tested this locally, and it seems to work, used `/http://localhost:8000/course/detail/%D8%AA%D8%AD%D9%84%DB%8C%D9%84_%D8%A8%DB%8C%D8%AA_%DA%A9%D9%88%DB%8C%D9%86/` and `/course/detail/تحلیل_بیت_کوین/` and it entered the function with the right value, perhaps something else is wrong with the `urls.py` on the upper level, or with the view itself. – Willem Van Onsem Nov 30 '22 at 13:37
  • @Alirezajavanpour: seems to work with Django-4.1 – Willem Van Onsem Nov 30 '22 at 13:38
  • I use the same version . and this is the error that i got . its something form urls.py : The current path, course/detail/%D8%AA%D8%AD%D9%84%DB%8C%D9%84-%D8%A8%DB%8C%D8%AA-%DA%A9%D9%88%DB%8C%D9%86/, didn’t match any of these. – Alireza javanpour Nov 30 '22 at 13:40
  • @Alirezajavanpour: and you have a "parent" url with `course/`? Can you edit the question with the "root" `urls.py`? – Willem Van Onsem Nov 30 '22 at 13:41
  • @Alirezajavanpour: the `urls` you show is in `courses.urls`? – Willem Van Onsem Nov 30 '22 at 13:45