Introduction to the actual application solution reported by projects in Python Django

Source: Internet
Author: User

If you are confused about the actual application and operation solutions of Python Django projects, you can use our article to provide a detailed introduction to the actual operations of Python Django projects, the following is a detailed description of the article. Hope you will have some gains.

Report in Python Django Project

 
 
  1. 'ascii' codec can't decode byte 0xe6 in position 
    15: ordinal not in range(128) 

Now let's track this BUG. Let's take a look at how to solve the first situation:

 
 
  1. try:  
  2. return HttpResponse("<script>alert);
    </script><script>window.location.href
    ='/serverinfolist/levels/?t_id="+app_id+"';</script>")  
  3. except Exception,e:  
  4. logging.debug(e)  

In this case, this error is reported. Initially, I felt that the problem was caused by JS writing! Online tracking: It is critical to remember that all strings in the Code use unicode, rather than str. In this way, you can clearly understand the string type to be processed. Remember, it's all, anywhere.

 
 
  1. > S1 = u '% s welcome' % u 'Beijing'
  2. > S1

Output:

 
 
  1. u'\u5317\u4eac\u6b22\u8fce\u4f60'  

The unicode encoding format is obtained.

 
 
  1. >>print s1 

Welcome to Beijing

 
 
  1. > S1 = '% s welcome' % 'Beijing'
  2. > S1

Output:

 
 
  1. '\xb1\xb1\xbe\xa9\xbb\xb6\xd3\xad\xc4\xe3'  

The returned string encoding> print s1 is to welcome you to Beijing. However, if you write >>s1 = u '% s,' % 'Beijing' is welcome:

 
 
  1. UnicodeDecodeError: 'ascii' codec can't decode 
    byte 0xb1 in position 0: or  
  2. not in range(128)   

If you write like this:> s1 = '% s, you are welcome to receive such an error like' % u'beijing! Conclusion: If you want to use the % s formatted output method, you must note that the encoding methods of both are consistent. Otherwise, this error will be reported!

It can be guessed by UnicodeDecodeError. The parser tries to use ascii to '% s. Welcome! ', Because' % s welcomes you! 'Actually uses UTF-8 encoding, which is the default value on our system terminal). Therefore, the error' % s will definitely be returned when ascii decoding is used. 'Welcome' the parser will try to decode it using ascii. An error occurred because the default value is UTF-8.

PS: the summary of a conversation is as follows:

Converting str to unicode is not directly constructed. It is the decode method of the str object. Str encoding is related to the system environment, which is generally sys. the unicode value obtained by getfilesystemencoding () can be stored no matter how it is stored. It is regarded as the text type in which the system can recognize the content, so it is converted from unicode to str, it is regarded as a process of encoding the information text into a binary byte stream. The encode method is used.

Conversion from str to unicode is considered to interpret meaningful information text from a binary string, so str in decode can be considered as an object for processing! Now I write the code as follows:

 
 
  1. Return
  2. HttpResponse ("<script> alert ('Operation successful! '); </Script>
  3. <Script> window. location. href = '/serverinfolist/levels /? T_id = % s'; </script> "% app_id.decode ('utf-8 '))
  4.  

Continue reporting. Think of the BT problem. Solution:

 
 
  1. return HttpResponseRedirect("/serverinfolist/levels/?t_id="+app_id) 

This will solve the problem.

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.