Reputation: 2158
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:
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 :-) :
[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()
Upvotes: 0
Views: 1776
Reputation: 3436
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 of webapp.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
$ env PYTHONPATH=/somewhere/google_appengine/lib/django_1_3 \
python /somewhere/google_appengine/lib/django_1_3/django/bin/django-admin.py\
startproject my_django_project
$ cd my_django_project
You can use settings.py for your django settings file by configuring an env_variables in your app.yaml.
Create an app.yaml
application: tmatsuo-hr
version: 1
runtime: python27
api_version: 1
threadsafe: true
env_variables:
DJANGO_SETTINGS_MODULE: 'settings'
handlers:
- url: /.*
script: main.app
libraries:
- name: django
version: 1.3
Create your django app
$ env PYTHONPATH=/somewhere/google_appengine/lib/django_1_3 \
python manage.py startapp myapp
Create your main.py
import django.core.handlers.wsgi
app = django.core.handlers.wsgi.WSGIHandler()
Configure your settings.py
ROOT_URLCONF = 'urls'
Add 'myapp' to your INSTALLED_APPS
Configure urls.py
url(r'^$', 'myapp.views.index', name='index'),
Create your views at myapp/views.py
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World!')
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.
Upvotes: 1