【问题标题】:Url Persian not found in django/passenger在 django/passenger 中找不到 Url Persian
【发布时间】:2020-07-27 07:37:07
【问题描述】:

示例网址:

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(r'^category/(?P<page_slug>.*)/$', views.category, name='category'),

乘客配置:

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 配置:

"""
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()

但在 url 中找不到响应 404! 所有 url 波斯蛞蝓都有问题。 将配置 wsgi 更改为取消引用时:

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)

把网址改成:

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/

但是有一个悬而未决的问题! 我应用了通过搜索找到的所有更改,但仍然存在问题!

输出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

【问题讨论】:

    标签: django-urls


    【解决方案1】:

    使用encode('utf-8') 表示波斯字符并在文件顶部设置# -*- coding: utf-8 -*-

    例如,当您在 DB 上保存数据或在 DB 上读取时使用此功能。

    即: slug.encode('utf-8')

    【讨论】:

      【解决方案2】:

      可以在视图中使用“uri_to_iri”,例如:

      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))
      

      【讨论】:

        猜你喜欢
        • 2013-07-13
        • 2014-11-18
        • 2017-01-18
        • 2020-04-14
        • 1970-01-01
        • 2013-10-29
        • 2014-06-12
        • 2019-01-22
        相关资源
        最近更新 更多