Reputation: 10015
I have an non ASCII character on HTML form data and when Flask processes the character, it gives me an error like this:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 2: ordinal not in range(128)
I believe that I have to decode the request form, but I can't find a way to do that.
Here's what I have:
<body>
<div id="avisos">
<form action="/opcion/avisos_cadastrar/resultado" method="post"> <br>
<fieldset>
<legend> Aviso </legend>
<center> <h3> Cadastrar </h3> </center>
<br>
Titulo: <input type="text" name="titulo" maxlength="32" autocomplete='off'> </input>
<textarea name="aviso" id="text-area" rows="10" cols="50" maxlength="512" autocomplete='off'> </textarea>
<br>
<input type=submit value="enviar">
<script>
var area = document.getElementById("text-area");
var message = document.getElementById("message");
var maxLength = 512;
var checkLength = function() {
if(area.value.length <= maxLength) {
message.innerHTML = (maxLength - area.value.length) + " caracteres restantes.";
}
}
setInterval(checkLength, 150);
</script>
</fieldset>
</form>
</div>
</body>
@app.route("/opcion/avisos_cadastrar/resultado", methods = ['POST'])
def avisos_cadastrar_resultado():
__titulo = request.form['titulo']
__aviso = request.form['aviso']
query_insert_aviso = " INSERT INTO tjs_stage.avisos (Titulo, Aviso) VALUES ('{} ', '{} ')" .format(__titulo,__aviso)
cur.execute(query_insert_aviso)
con.commit()
return render_template('resultado.html')
I tried to use something like..
__titulo = request.form['titulo'].decode("utf-8")
__aviso = request.form['aviso'].decode("utf-8")
...and also...
__titulo = request.form['titulo'].decode("raw_unicode_escape")
__aviso = request.form['aviso'].decode("raw_unicode_escape")
...but it didn't worked.
Maybe something is missing at my HTML or maybe at the FlaskApp, but I'm little bit lost.
Any ideas?
Upvotes: 1
Views: 1580
Reputation: 576
Does the traceback indicate that your error is in fact coming from the lines where you set the value of __titulo
and __aviso
? Seems more likely that its coming from cur.execute()
. Also you need to be careful when executing SQL queries with data from forms. You should escape that data to prevent any potential SQL injection attacks from malicious users
To test this problem, let's try changing the default character encoding in python to unicode as described here. To do this add the following lines to the top of your script:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
However in general the use of this method is discouraged. There are multiple more permanent solutions address in this stack overflow question but the "real" answer is that you should upgrade to python 3 where the default character encoding is already Unicode.
Hope this helps!
Upvotes: 3