从此代码块:
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 值?还是这里有其他我没有看到的错误?