I have followed the django documentation[1] to implement i18n on a “hello word” kind of google app engine website.
unfortunately, after reading dozens of html pages, django and appengine documentation I can’t figure out what is happening:
- changing the language code into the django_settings.py do change the language of the page and update a dropdown with the current language (i.e. LANGUAGE_CODE = ‘fr’ or ‘es’ or ‘en’)
- change the dropdown and clicking on “change language” do lead to a “error 404” and the URL shows http:///i18n/setlang/
where can I find a list of django middlewares that app engine use by default? this might help me to digg deeper.
technical requirements (we don’t plan to upgrade before i18n works 🙂 :
- python 2.5.4
- google app engine 1.6.2
- Django 1.2
[1] [https://docs.djangoproject.com/en/1.2/topics/i18n/]
[2] [cssjanus.googlecode.com] a piece of code that do exactly what I want to do. but I miss a small trick
index.html contains this
<form action="/i18n/setlang/" method="post">
<input name="next" type="hidden" value="/MainPage">
<select name="language">
{% for lang in LANGUAGES %} <option value="{{ lang.0 }}"
{% ifequal LANGUAGE_CODE lang.0 %}
selected="selected"
{% endifequal %}>{{ lang.1 }}</option>
{% endfor %}
</select>
<input type="submit" value="{% trans "Change Language" %}">
</form>
app.yaml:
application: i18n
version: 1
runtime: python
api_version: 1
handlers:
- url: .*
script: helloworld.py`
django_seetings.py
import os
DEBUG = True
TEMPLATE_DEBUG = DEBUG
LANGUAGE_CODE = 'fr'
USE_I18N = True
gettext = lambda s: s
LANGUAGES = (
('en', gettext('English')),
('fr', gettext('French')),
('es', gettext('Spanish')),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
)
SESSION_ENGINE = 'gae_sessions'
helloworld.py (I don’t use urls.py)
# coding=UTF-8
# Standard Python imports.
import os
import sys
import logging
import __builtin__
# Google App Hosting imports.
from google.appengine.dist import use_library
use_library('django', '1.2')
# Enable info logging by the app (this is separate from appserver's
# logging).
logging.getLogger().setLevel(logging.INFO)
# Must set this env var *before* importing any part of Django.
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_settings'
from google.appengine.ext.webapp import util
# Import the part of Django that we use here.
from google.appengine.ext import webapp
from views import MainPage
from django.conf.urls.defaults import include
def main():
# Create a Django application for WSGI
application = webapp.WSGIApplication([('/', MainPage),
(r'^i18n/', include('django.conf.urls.i18n')),
], debug=True)
# Run the WSGI CGI handler with that application.
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
views.py
'''
Created on Apr 24, 2012
@author:xxxx
'''
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from django.utils.translation import ugettext #ok
from django import http
from django.http import HttpResponseRedirect
import django_settings
from django.utils.translation import check_for_language
#from django.shortcuts import render_to_response
#def MainPage(request):
class MainPage(webapp.RequestHandler):
def get(self):
template_values = {
'helloworld': ugettext("helloworld!"),
'title': ugettext("home page"),
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
#return render_to_response('index.html', template_values)
helloworld.py
# coding=UTF-8
# Standard Python imports.
import os
import sys
import logging
import __builtin__
# Google App Hosting imports.
from google.appengine.dist import use_library
use_library('django', '1.2')
# Enable info logging by the app (this is separate from appserver's
# logging).
logging.getLogger().setLevel(logging.INFO)
# Must set this env var *before* importing any part of Django.
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_settings'
from google.appengine.ext.webapp import util
# Import the part of Django that we use here.
from google.appengine.ext import webapp
from views import MainPage
from django.conf.urls.defaults import include
def main():
# Create a Django application for WSGI
application = webapp.WSGIApplication([('/', MainPage),
(r'^i18n/', include('django.conf.urls.i18n')),
], debug=True)
# Run the WSGI CGI handler with that application.
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
It seems that you are confusing django routing with webapp’s one. I don’t think you can use statement like
include('django.conf.urls.i18n')in the initialization process ofwebapp.WSGIApplication.Additionally, as greg says, I also recommend using Python2.7 runtime, because it is much easier for you to use django(Believe me, it’s super easy) with the new runtime.
Updated: added a procedure for running django-1.3 with Python2.7 runtime
Here is a rough procedure to make django work with python2.7 runtime.
Create a project
You can use settings.py for your django settings file by configuring
an env_variables in your app.yaml.
Create an app.yaml
Create your django app
Create your main.py
Configure your settings.py
Configure urls.py
Create your views at myapp/views.py
Done. You should be able to configure this project in the same way as usual django applications.
However, you can not use Django’s model because it needs SQL backends. If you’d like to do so, go check django-nonrel, or consider using django with CloudSQL.