简体   繁体   English

mysql字段转换为HTML模板(python unicode解码错误)

[英]mysql fields to HTML template (python unicode decode error)

I need to send fields from a mysql query to a HTML template select. 我需要将字段从mysql查询发送到HTML模板选择。 This is my python code: 这是我的python代码:

class Estilos(webapp2.RequestHandler):
def get(self):
    db = .....
    cursor = db.cursor()
    cursor.execute('SELECT estilo, n1 FROM estilos;')
    lista = []
    for row in cursor.fetchall():
     lista.append([cgi.escape(row[0]),
                  cgi.escape(row[1])
                  ])

    variables = {'lista':lista}

    template = JINJA_ENVIRONMENT.get_template('estilo.html')
    self.response.write(template.render(variables))
    db.close()    

This is my HTML template: 这是我的HTML模板:

<html>
  <form action="/sign" method="post">
    <br />
    <div>Estilo: <select name="estilo" style="width:400px"> 
        {% for e in lista %}
              <option value="{{ e }}">{{ e }}                      
              </option>
            {% endfor %}
             </select>
             </div>

    <div> N1: <select name="n1" style="width:400px"> 
        {% for e in lista %}
              <option value="{{ e }}" >{{ e }}                      
              </option>
            {% endfor %}
             </select>
             </div>

    <div><input type="submit" value="Selecionar"></div>
  </form>    

This code shows the entire row in the select field: ['STRONG PALE ALE', 'ALE'] 此代码在选择字段中显示整行:['STRONG PALE ALE','ALE']

When I change the form select value to 当我将表单选择值更改为

                  <option value="{{ e[0] }}">{{ e[0] }}                      
              </option>    

then I get a unicode decode error. 然后我得到一个unicode解码错误。

<option value="{{ e[0] }}">{{ e[0] }} UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 1: ordinal not in range(128)    

I have tried to encode the cgi.escape to utf8 but I cannot make it work. 我尝试将cgi.escape编码为utf8,但无法使其正常工作。 Does anybody know how to fix this? 有人知道如何解决此问题吗? What is the correct way to encode the string? 编码字符串的正确方法是什么?

Taking literally every detail reported (ie assuming you made absolutely no mistakes in reporting nor mixed up different versions of your code) I'd be stumped: that \\xc4 (uppercase A with umlaut, if eg latin-1) springs out of nowhere 从字面上理解报告的每个细节(即假设您在报告中完全没有错误,也没有混淆代码的不同版本),我会感到很\\xc4\\xc4 (大写字母A,带有\\xc4 ,例如latin-1)从任何地方冒出来

However, the OP confirmed in a comment that he was actually mixing up different versions of the code, in which case...: 然而,在OP评论证实,他实际上混合了不同版本的代码,在这种情况下... ...:

A good idea is to use cgi.escape(row[0].decode('ascii','replace')) &c, and see where question marks replace characters that were expected. 一个好主意是使用cgi.escape(row[0].decode('ascii','replace')) &c,并查看问号在哪里替换期望的字符。 The OP confirmed in a comment this worked for them. OP在评论中确认这对他们有用。

For completeness, thinking of future readers with similar problems: if decode shows nothing, they could try encode instead, but that will likely give the same UnicodeDecodeError originally reported by the OP. 为了完整起见,请考虑有类似问题的未来读者:如果decode没有显示任何内容,则他们可以尝试encode ,但是可能会给出与OP最初报告的相同的UnicodeDecodeError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM