Issue
My Sample Python Script is like this:
# -*- coding: utf-8 -*-
from flask import *
app = Flask(__name__)
@app.route('/', methods=['GET','POST'])
def checkName():
if request.method=='POST':
namekh = request.form['KhmerName']
print "Khmer name is ",namekh
if isinstance(namekh.encode('utf8'), unicode):
return render_template('hello.html', Name=namekh)
else:
namekh = 'Please enter khmer character only'
return render_template('hello.html', Name=namekh)
return render_template('hello.html')
if __name__ == '__main__':
app.run(debug=True)
From script above, I tried to received input value from form element name KhmerName
on submit and check if it is a Unicode Character or not. Then I send it to display at my html markup hello.html
.
The html is look like this:
{% if Name %}
<p>Hello, {{Name}} wellcome to mysite</p>
{% endif %}
<form class="form-horizontal" action='' method='POST'>
<div class="form-group">
<label for="KhmerName" class="col-sm-2 control-label">Khmer Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="KhmerName" name="KhmerName" placeholder="KhmerName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
However, my problem is that if KhmerName
is kind of None-Character is it working fine, yet it is an Unicode Character, it will return an error message
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-4: character maps to <undefined>
Traceback (most recent call last)
File "C:\Python27\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "F:\python\check Unicode\hello.py", line 12, in hello_world
print "Khmer name is ",namekh
File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-4: character maps to <undefined>
What I did to check the Unicode here is by using isinstance(namekh.encode('utf8'), unicode)
, so it should working (return true) because the type of namekh
was already an unicode
type if an input value was an unicode character. Yet, I had no idea why it return an error like above.
Solution
I have test on a demo project. The type of variable return from request.form["key"]
is unicode
. So you needn't transform it to unicode
from str
use decode
. I also test សួរស្តីរ
on the demo project, it can be printed. From the code you provide. You might want to only allow the khmer character. I think you use regex to test the input.
REGEX_KHMER = u"[\u1780-\u17dd\u17e0-\u17e9\u17f0-\u17f9]+"
if re.match(REGEX_KHMER, namekh):
return correctly
else:
return enter khmer character only
Answered By - stamaimer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.