このコード ブロックから:
class MainHandler(webapp2.RequestHandler):
def get(self):
template_values = {"given_sentence":'put a sentence here'}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
def post(self):
nb = naivebayes(getfeatures)
sampletrain(nb)
given_sentence = self.request.get("given_sentence").encode('utf-8')
# given_sentence = self.request.get("given_sentence")
spam_result = nb.classify(given_sentence)
submit_button = self.request.get("submit_button")
if submit_button:
self.redirect('/test_result?spam_result=%s&given_sentence=%s' % (spam_result, given_sentence))
次のエラーが表示されます。
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 262, in post
self.redirect('/test_result?spam_result=%s&given_sentence=%s' % (spam_result, given_sentence))
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 608, in redirect
response=self.response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1767, in redirect
uri = str(urlparse.urljoin(request.url, uri))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 82: ordinal not in range(128)
INFO 2012-08-16 20:02:52,229 dev_appserver.py:2952] "POST / HTTP/1.1" 500 -
INFO 2012-08-16 20:05:06,858 py_zipimport.py:148] zipimporter('C:\\Python27\\lib\\site-packages\\pyyaml-3.10-py2.7-win32.egg', '')
ERROR 2012-08-16 20:05:08,230 webapp2.py:1553] 'ascii' codec can't decode byte 0xc3 in position 16: ordinal not in range(128)
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 262, in post
self.redirect('/test_result?spam_result=%s&given_sentence=%s' % (spam_result, given_sentence))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 16: ordinal not in range(128)
の値given_sentence
が次のような場合: productos farmacéuticos comprar ahora
.
私のpythonコードはutf-8です。テンプレートにも (<head>
タグ内に) この行を追加しました: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
.
問題は、URL を介して非 ASCII 文を渡すことにあるようです。これを試してみると ("productos comprar ahora")、問題ありません:
http://localhost:8084/test_result?spam_result=good&given_sentence=productos%20comprar%20ahora
しかし、これを試してみると、「productos farmacéuticos comprar ahora」というエラーが表示されました。url に非 ASCII 値を渡すことはできませんか? それとも、私が見ていない他の間違いがありますか?