Google App Engine Tutorial – Datastore – 한글 입력시 Internal Server Error 나는 현상

Read Time:3 Minute, 15 Second

Google App Engine의 Datastore 튜토리얼 소스를 실행시 한글을 방명록 이름(Guestbook name)필드에 입력하면 다음과 같은 에러가 발생한다.

Traceback (most recent call last):
  File "/Applications/", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/", line 1102, in __call__
    return handler.dispatch()
  File "/Applications/", line 572, in dispatch
    return self.handle_exception(e,
  File "/Applications/", line 570, in dispatch
    return method(*args, **kwargs)
  File "/Applications/MAMP/htdocs/google_app_engines/datastore/", line 65, in get
    sign_query_params = urllib.urlencode({'guestbook_name': guestbook_name})
  File "/Applications/", line 1307, in urlencode
    v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-2: ordinal not in range(128)

위 에러가 발생하는데, 이걸 해결하기 위해 다음과 같은 글을 찾았고, 한글 입력시에도 방명록 이름 바꾸는게 가능하게 되었다.

class Greeting(db.Model):
    """Models an individual Guestbook entry with author, content, and date."""
    author = db.StringProperty()
    content = db.StringProperty(multiline=True) # multipe lines
    date = db.DateTimeProperty(auto_now_add=True)   # add now time value automatically

def guestbook_key(guestbook_name=None):
    """Constructs a Datastore key for a Guestbook entity with guestbook_name."""
    guestbook_name = guestbook_name.decode("utf-8")
    return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')

class MainPage(webapp2.RequestHandler):

    def get(self):
        guestbook_name = self.request.get('guestbook_name').encode("utf-8")

        # Ancestor Queries, as shown here, are strongly consistent with the High
        # Replication Datastore. Queries that span entity groups are eventually
        # consistent. If we omitted the ancestor from this query there would be
        # a slight chance that Greeting that had just been written would not
        # show up in a query.

        greetings = db.GqlQuery("SELECT * "
                                "FROM Greeting "
                                "WHERE ANCESTOR IS :1 "
                                "ORDER BY date DESC LIMIT 10",

        for greeting in greetings:
                    '%s wrote:' %
                self.response.write('An anonymous person wrote:')
            self.response.write('%s' %

        # Write the submission form and the footer of the page
        sign_query_params = urllib.urlencode({'guestbook_name': guestbook_name})
        self.response.write(MAIN_PAGE_FOOTER_TEMPLATE %
                            (sign_query_params, cgi.escape(guestbook_name)))

# /sign
class Guestbook(webapp2.RequestHandler):

    def post(self):
        # We set the same parent key on the 'Greeting' to ensure each greeting
        # is in the same entity group. Queries across the single entity group
        # will be consistent. However, the write rate to a single entity group
        # should be limited to ~1/second.

        guestbook_name = self.request.get('guestbook_name').encode("utf-8")
        greeting = Greeting(parent=guestbook_key(guestbook_name))

        if users.get_current_user():
   = users.get_current_user().nickname()

        greeting.content = self.request.get('content')

        query_params = {'guestbook_name': guestbook_name}
        # urllib.urlencode : GET 문자열로 변환해줌 
        self.redirect('/?' + urllib.urlencode(query_params))

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/sign', Guestbook)],

import 및 템플릿 코드는 위에서 제외됨.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.