0

我已经按照 django 文档 [1] 在“hello word”类型的谷歌应用引擎网站上实现 i18n。

不幸的是,在阅读了几十个 html 页面、django 和 appengine 文档后,我无法弄清楚发生了什么:

  • 将语言代码更改为 django_settings.py 确实更改页面的语言并使用当前语言更新下拉列表(即 LANGUAGE_CODE = 'fr' 或 'es' 或 'en')
  • 更改下拉菜单并单击“更改语言”确实会导致“错误 404”并且 URL 显示 http:///i18n/setlang/

我在哪里可以找到应用引擎默认使用的 django 中间件列表?这可能有助于我深入挖掘。

技术要求(我们不打算在 i18n 工作之前升级 :-):

  • 蟒蛇2.5.4
  • 谷歌应用引擎 1.6.2
  • 姜戈 1.2

[1] [https://docs.djangoproject.com/en/1.2/topics/i18n/]

[2] [cssjanus.googlecode.com] 一段代码,正是我想做的事情。但我错过了一个小技巧

index.html 包含这个

<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>

应用程序.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(我不使用 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()

视图.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)  

你好世界.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()
4

1 回答 1

1

似乎您将 django 路由与 webapp 的路由混淆了。我不认为你可以像include('django.conf.urls.i18n')在初始化过程中那样使用语句webapp.WSGIApplication

此外,正如 greg 所说,我还建议使用 Python2.7 运行时,因为在新的运行时中使用 django(相信我,这非常容易)对您来说要容易得多。

更新:添加了使用 Python2.7 运行时运行 django-1.3 的过程

这是使 django 与 python2.7 运行时一起工作的粗略过程。

创建项目

$ 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

您可以通过在 app.yaml 中配置 env_variables 将 settings.py 用于 django 设置文件。

创建一个 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                                                                                                                                                 

创建你的 Django 应用

$ env PYTHONPATH=/somewhere/google_appengine/lib/django_1_3 \
   python manage.py startapp myapp                                                                    

创建你的 main.py

import django.core.handlers.wsgi

app = django.core.handlers.wsgi.WSGIHandler()

配置你的 settings.py

ROOT_URLCONF = 'urls'
Add 'myapp' to your INSTALLED_APPS

配置 urls.py

url(r'^$', 'myapp.views.index', name='index'),

在 myapp/views.py 创建您的视图

# Create your views here.

from django.http import HttpResponse

def index(request):
    return HttpResponse('Hello World!')

完毕。您应该能够以与通常的 django 应用程序相同的方式配置此项目。

但是,您不能使用 Django 的模型,因为它需要 SQL 后端。如果您想这样做,请检查 django-nonrel,或考虑将 django 与 CloudSQL 一起使用。

于 2012-04-26T08:34:41.190 回答