Python Django upload file name for Chinese error

Source: Internet
Author: User
Tags locale

The above is the error screenshot and log files can be found on the Internet relatively reliable solution is the following, but the following method can only solve the request parameters in Chinese and then Chinese garbled problem, can not handle uploading Chinese name file save errors. Two error position 0-4 unicodeencodeerror: & #39;ascii& #39; codec can& #39 T encode Characters in position 0-4: Ordinal isn't in range (128)

First say the solution:?

1 2 3 Import sys reload (SYS) sys.setdefaultencoding (' Utf-8 ')
That is, to import the SYS package before the code file starts, and then invoke the method in your code to modify the default encoding setdefaultencoding
Here's why: This problem is caused by the incompatibility of Unicode encoding with ASCII encoding.
First look at the default encoding for Python calls without adding the above code:
?
1 2 Import sys print sys.getdefaultencoding ()
Typically ASCII, this Python naturally invokes the ASCII codec to handle the character stream, and throws an exception (ordinal not in range (128)) when the character stream is not within the ASCII range. So the workaround is to modify the default encoding, and note that the reload method needs to be invoked first.
?
1 2 Reload (SYS) sys.setdefaultencoding (' Utf-8 ')

This article for Eliot original, reprint please indicate the source: http://blog.csdn.net/xyw_blog/article/details/18214399?
1 2 3 <pre class = "Brush:java ></pre> <pre class =" Brush:java; "></pre> <pre class =" Brush:ja VA; "></pre>

The above is the solution to the request parameters in the Chinese method
The file name is the Chinese upload error correction method The core idea is to modify the operating system default character set, the results are as follows
Execute sudo locale-gen command

Configures the environment variables for the current user. BASHRC Add the following configuration
View current configuration

enable Ubuntu to display GB2312, GBK encoded files normally
Ubuntu Environment set the character set utf8,windows the default character set is the Gbk,ubuntu default character set for Utf-8, which makes

Garbled when you log on to a remote server or view a Windows file with Telnet. You need to set the Ubuntu environment to GBK or GB2312, or set up the software to display the characters correctly. The following is an example of the GBK character set:

First, modify the Ubuntu default character set for GBK
1, first set sudo vi/var/lib/locales/supported.d/local
Add a row of ZH_CN. GBK GBK
sudo locale-gen generation locale

2, modify the character set of Ubuntu
Method One: Modify the. profile file under the user directory and add the following:
Language= "Zh_CN:zh:en_US:en"
Lang=zh_cn. GBK
Log in again.
This method is only valid for the user.
Method Two: Modify the/etc/environment, add the following content:
Language= "Zh_CN:zh:en_US:en"
Lang=zh_cn. GBK
and then reboot X.
This method works for users who do not have the Lang and language environment variables set

Second, set the software to name its normal display GBK
This requires the software itself to support multiple character encoding, the most common is IE and other browsers, you can set the encoding display GBK character set.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.