Djangoweb Application _mysql_cmd Chinese garbled problem

Source: Internet
Author: User

Currently using Django, MySQL to learn, applied to the "simple Poll" website, in the course of learning, encountered the Chinese garbled problem, is now recorded as follows:

System conditions: Win8, mysql5.5, python2.7, django1.11

  1. Start by following the tutorial for MySQL database configuration, in the character option, select the second item, which is UTF8 encoding. After the configuration is complete, after the database server is linked in CMD, the Operation show variables like ‘%character%‘ display is correctly encoded-almost all UTF8. After you have determined the above information, you can access the Web app in your browser to display Chinese characters correctly, but you cannot click on ' Chinese question ' to go to the detailed ' English options '. The command operation, in MySQL's own MySQL command line client (similar to window with CMD) in the query found Chinese characters garbled.

  2. In MySQL command line client, that is, cmd in the database some editing operations, found unable to insert Chinese characters--direct error-incorrect string ...
    1. Find materials, most recommend changing the Default-character-set=utf8 in profile My.ini to DEFAULT-CHARACTER-SET=GBK , where you use The show variables like '%character% ' command can view encoding issues. After that, you can use insert INTO ... Insert Chinese characters, can also be queried in cmd, display correct, no garbled, the browser display is normal, but can not click to see the details-error unicodedecodeerror.

      Mysql> Show variables like '%character% '; +--------------------------+------------------------------------------ ---------------+| variable_name | Value |+--------------------------+------------------------------------- --------------------+| character_set_client | GBK | | character_set_connection | GBK | | Character_set_database | UTF8 | | Character_set_filesystem | binary | | Character_set_results | GBK | | Character_set_server | UTF8 | | Character_set_system | UTF8 | | Character_sets_dir | C:\Program files\mysql\mysql Server 5.5\share\charsets\ |+--------------------------+---------------------------------------------------------+8 rows in Set (0.00 sec) 
  3. Check the encoding of the Web app, # -*- coding:utf-8 -*- change all to GBK, run again, found that the cmd insert operation is displayed correctly, in the browser access to admin, click on the Chinese issue, or the above error--unicodedecodeerror.

  4. From the above 1, 2, 3 we can see that the client, Web application and database connection involved, these three have their own coding problems. For the client display Chinese characters, we must set to GBK encoding, whether the client code is GBK or UTF8 (prepared to say Default-character-set=gbk/utf8), the browser display is normal, which indicates that the database is OK, the browser is not a problem. So the problem is with our web application.

  5. or coding problems, continue to Baidu problem, most still remind write character set, database character set, data table character set three unified , respectively corresponding to the Web application, database, database, but the actual situation involves the Web application, database and cmd the coding problem, Then start in cmd python manage.py shell to encode the query import sys-sys.getdefaultencoding() #ascii displayed as ascii! is not our utf8, so, sys.setdefaultencoding(‘utf-8‘) because this setting cannot be permanently in the Web application, so add the following code in the manage.py, reboot together normal! This time regardless of Default-character-set=gbk/utf8, it affects the client display is garbled, but does not affect the Web app display and database links.

    #...import sys reload(sys)sys.setdefaultencoding(‘utf-8‘)#...
Attention
    1. In the final analysis, the application, database encoding, write character set these three must be unified, although here, in order to display Chinese in the CMD client and insert the Chinese language, its encoding is set to GBK, this is only affect the client display problem, the display in the browser is not affected.
    2. Now look back, the error unicodedecodeerror, should focus on the application of the code, although we have each file head -*-conding:utf-8-*- , but the Chinese browser, database, etc., not necessarily all UTF8 encoding, the data exchange between their links will lead to coding problems, So add the 4th code above in the Web application.
    3. In our actual operation, CMD in the manipulation of the database is not a lot of it, in which to insert less data, so it is best to all unified for UTF8 code bar. Mysqlyog visualization software can be displayed normally.

Djangoweb Application _mysql_cmd Chinese garbled problem

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.