python---补充django中文报错(1),Django2.7使用sys.setdefaultencoding('utf-8'),以及使用reload(sys)原因

SyntaxError at /blog/ news/story
Non-ASCII character '\xe4' in file D:\MyPython\day23\HelloWorld\blog\views.py on line 42, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details (views.py, line 42)

当使用中文时会报错:

def introduce(req):
    return HttpResponse("<h1>ok你</h1>")

其解决方法是:在文件前面加入

#coding:utf-8

其实不止在Django,在python文件中开头为了支持中文我们都应该加上

#coding:utf-8

在初学时一直使用,只不过后来慢慢忘了,那时候使用的是:

# -*- coding:utf-8 -*-

当然,这两个都是可以使用的

另外:该注释最好放在文件开始,不然会无效

以上为回顾,下面开始重点!!!

 

重点+补充:上面只是针对某个文件下对应功能的编码,对于整个项目则用处不大,需要重新调整

在项目启动文件manage.py中修改系统的默认文件编码

原来文件编码是:python安装目录的Lib文件夹下,有一个叫site.py的文件

首先了解一下site.py模块: https://docs.python.org/2/library/site.html

文中说到This module is automatically imported during initialization

这个模块将在解释器启动的时候自动执行。

所有开始了解这个模块:

主函数:

def main():
    global ENABLE_USER_SITE

    abs__file__()
    known_paths = removeduppaths()
    if ENABLE_USER_SITE is None:
        ENABLE_USER_SITE = check_enableusersite()
    known_paths = addusersitepackages(known_paths)
    known_paths = addsitepackages(known_paths)
    if sys.platform == 'os2emx':
        setBEGINLIBPATH()
    setquit()
    setcopyright()
    sethelper()
    aliasmbcs()
    setencoding()
    execsitecustomize()
    if ENABLE_USER_SITE:
        execusercustomize()
    # Remove sys.setdefaultencoding() so that users cannot change the
    # encoding after initialization.  The test for presence is needed when
    # this module is run as a script, because this code is executed twice.
    if hasattr(sys, "setdefaultencoding"):
        del sys.setdefaultencoding

main()  #在模块中自动调用该函数

先了解setencoding():设置了默认编码为ASCII  encoding = "ascii"

def setencoding():
    """Set the string encoding used by the Unicode implementation.  The
    default is 'ascii', but if you're willing to experiment, you can
    change this."""
    encoding = "ascii" # Default value set by _PyUnicode_Init()
    if 0:
        # Enable to support locale aware default string encodings.
        import locale
        loc = locale.getdefaultlocale()
        if loc[1]:
            encoding = loc[1]
    if 0:
        # Enable to switch off string to Unicode coercion and implicit
        # Unicode to string conversion.
        encoding = "undefined"
    if encoding != "ascii":
        # On Non-Unicode builds this will raise an AttributeError...
        sys.setdefaultencoding(encoding) # Needs Python Unicode build !

可以看到默认文件编码是ASCII编码

也可以使用sys模块中getdefaultencoding()获取默认编码

print(sys.getdefaultencoding())#ascii

重点:

if hasattr(sys, "setdefaultencoding"):
        del sys.setdefaultencoding

因为这个site.py每次启动python解释器时会自动加载,所以main函数每次都会被执行,setdefaultencoding函数一出来就已经被删除了。

所以通过import引用进来时其实已经没有了,所以必须reload一次sys模块,这样setdefaultencoding才会为可用,才能在代码里修改解释器当前的字符编码。

当没有使用reload时:会无法找到setdefaultencoding该函数

#AttributeError: 'module' object has no attribute 'setdefaultencoding'

所有要想在项目中设置编码:

需要在启动文件manage.py中设置:

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "admin.settings")
   reload(sys)
   sys.setdefaultencoding('utf-8')

之后就可以在项目中肆无忌惮的使用中文了

补充:使用的是python2.7  Django1.11.11

对于低版本Django1.8,使用过发现上面的reload和setdefaultencoding可以修改项目编码为utf-8,但是中文依旧显示不了

python---补充django中文报错(2),Django3.5出错

posted @ 2018-03-24 23:54  山上有风景  阅读(594)  评论(0编辑  收藏  举报