webappでdjangoのフォームを使うときにエラーメッセージを日本語にする

AppEngineのwebappでdjangoのフォームを使うと、settings.LANGUAGE_CODEがen-usなのでエラーメッセージが英語になって残念ですよね。
その解決方法など。

main.py

単純なテンプレートでフォームを表示してis_validするだけのコード。

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from django import newforms as forms


class TestForm(forms.Form):
    name = forms.CharField()


class MainHandler(webapp.RequestHandler):
    def get(self):
        template_values = {'form': TestForm()}
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

    def post(self):
        form = TestForm(self.request.POST)
        if form.is_valid():
            self.response.out.write('ok')
        else:
            template_values = {'form': form}
            path = os.path.join(os.path.dirname(__file__), 'index.html')
            self.response.out.write(template.render(path, template_values))


def main():
    application = webapp.WSGIApplication([('/', MainHandler)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()

index.html

<html>
<head>
  <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
  <title>フォームテスト</title>
</head>
<body>
  <form action="" method="post">
    <div>{{ form.as_p }}</div>
    <div><input type="submit" value="送信" />
  </form>
</body>
</html>

これで動かすとこんな感じで英語で表示される。

appengine_config.py

appengine_config.pyでdjango_setupを置き換えることでLANGUAGE_CODEを指定できます。

def webapp_django_setup():
    import django
    import django.conf
    django.conf.settings.configure(
        DEBUG=False,
        TEMPLATE_DEBUG=False,
        TEMPLATE_LOADERS=(
          'django.template.loaders.filesystem.load_template_source',
        ),
        LANGUAGE_CODE='ja'
    )

これで日本語表示でエラーメッセージが表示されました。めでたし。