action =“/ i18n / setlang /”不会重定向到任何页面

时间:2012-04-24 22:17:29

标签: django google-app-engine internationalization django-views

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

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

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

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

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

  • 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]一段完全符合我要求的代码。但我错过了一个小技巧

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>

的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(我不使用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()

1 个答案:

答案 0 :(得分: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

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

创建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一起使用。