Home > Mobile >  not found url when use persian/arabic slug in django
not found url when use persian/arabic slug in django

Time:08-04

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?

CodePudding user response:

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()
  • Related