djiango 中unicode 和str 方法转换的问题

来源:互联网 发布:最新ppt制作软件 编辑:程序博客网 时间:2024/06/11 00:40

写django 时候发现,作者为了保证最好的可用性,统一使用了unicode 编码。

使用django 中的模块,它会自动转为合适的类型。

而自己的代码如果直接去调用底层的接口,则可能出现错误。

例如在调用sockets.py这个底层的文件中的create_connection()方法时,传递二个参数,host,port.这两个参数在django中赋值是默认为unicode。而sockets则要求为int 或者 str

from django.utils.encoding import smart_unicodefrom django.utils.encoding import smart_strhost=smart_unicode('192.168.10.1')print type(host)host=smart_str('192.168.10.1')print type(host)

完美解决咯。

阅读全文
0 0
原创粉丝点击