Url Persian not found in django/passenger

Viewed 143

sample Url:

site.com/category/%D9%81%D8%AA%D9%88%DA%AF%D8%B1%D8%A7%D9%81%DB%8C_%D8%B1%DB%8C%D9%86%D9%88%D9%BE%D9%84%D8%A7%D8%B3%D8%AA%DB%8C/

url config:

url(r'^category/(?P<page_slug>.*)/$', views.category, name='category'),

passenger config:

import imp
import os
import sys


sys.path.insert(0, os.path.dirname(__file__))

wsgi = imp.load_source('wsgi', 'photography/wsgi.py')
application = wsgi.application

wsgi config:

"""
WSGI config for photography project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "photography.settings")
#os.environ["DJANGO_SETTINGS_MODULE"] = "photography.settings"

application = get_wsgi_application()

but response 404 not found in url! in problem for all url persian slug. when change config wsgi to unquote:

from urllib.parse import unquote

def application(environ, start_fn):
    environ['PATH_INFO'] = unquote(environ['PATH_INFO'])
    app = get_wsgi_application()
    print(environ)
    return app(environ, start_fn)

change url into:

site.com/category/%C3%99%C2%81%C3%98%C2%AA%C3%99%C2%88%C3%9A%C2%AF%C3%98%C2%B1%C3%98%C2%A7%C3%99/tag/%D9%81%D8%AA%D9%88%DA%AF%D8%B1%D8%A7%D9%81%DB%8C-%D9%BE%D8%B2%D8%B4%DA%A9%DB%8C/

enter image description here

But there is an open problem ! I applied all the changes I found with the search, but there is still a problem!

Output one of the changes in the wsgi:

App 3585081 output:     set_script_prefix(get_script_name(environ))
App 3585081 output:   File "/home/sepandteb/virtualenv/sepandteb/3.5/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 210, in get_script_name
App 3585081 output:     return script_name.decode(UTF_8)
App 3585081 output: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 27: unexpected end of data
2 Answers

use encode('utf-8') for Persian character and set # -*- coding: utf-8 -*- on top of file.

for example when you save data on DB or read on DB use this function.

i.e : slug.encode('utf-8')

You can use "uri_to_iri" in the view, for example:

from django.shortcuts import get_object_or_404
from django.utils.encoding import uri_to_iri 

def blog_detail(request, slug):
    post= get_object_or_404(Post, slug=uri_to_iri(slug))
Related