not found url when use persian/arabic slug in django

Question:

database: mysql
database Collation encode: ‘utf8_general_ci’
django version: last version
python version: 3.7.12

Note : It work well in local host but not working in real host

Error :

The error

models.py :

class blog(models.Model):
    slug = models.SlugField(max_length=255,allow_unicode=True,unique=True)
    title = models.CharField(max_length=255)
    description = models.CharField(max_length=255)

    def __str__(self):
        return  f'title: {self.title}'

Views.py :

def blogViews(request,slug):
    if blog.objects.filter(slug=slug).count() > 0:
        post = blog.objects.get(slug=slug)
        context = {
            'post': post,
        }
        return render(request,'blog/post_detail.html',context)
    else:
        return HttpResponseNotFound()

i tried these:

1- change get_bytes_from_wsgi encode in django/core/handlers/wsgi.py

return value.encode('ISO-8859-1')

to

return value.encode('UTF-8')

2- django setings.py:

ALLOW_UNICODE_SLUGS = True

How do I fix it?

Asked By: amirmohammad

||

Answers:

The problem was my url regex

model.py :

from django.utils.text import slugify

class blog(models.Model):
    slug = models.SlugField(max_length=255,allow_unicode=True,unique=True)
    title = models.CharField(max_length=255)
    description = models.CharField(max_length=255)
    
    def save(self, *args, **kwargs):
        # Optional
        self.slug = slugify(self.title, allow_unicode=True)
        super(blog, self).save(*args, **kwargs)

    def __str__(self):
        return  f'title: {self.title}'

urls.py :

from django.urls import re_path


urlpatterns = [
    # the main problem was here
    re_path(r'^blog/(?P<slug>[^/]+)/?$', blogtagsViews),
    
]

Views.py :

from urllib.parse import unquote

def blogViews(request,slug):
    slug = unquote(slug)
    if blog.objects.filter(slug=slug).count() > 0:
        post = blog.objects.get(slug=slug)
        context = {
            'post': post,
        }
        return render(request,'blog/post_detail.html',context)
    else:
        return HttpResponseNotFound()
Answered By: amirmohammad

Hex D8AAD8B3D8AA, when decoded as UTF-8 is Arabic تست — is that what you expected?

It looks like the string went through more than one encoding — at least once to turn into %D8%AA... — as if with PHP’s urlencode().

Then it seems to have gone through it again — to get the %25s.

You tagged this with [mysql]; if that is involved, please do a SELECT HEX(...) to see if the "double-encoding" happened when inserting into the database.

Answered By: Rick James
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.