Django setLang不处理“Next”参数

时间:2013-01-27 20:45:51

标签: django translation

我正在尝试使用以下代码实现动态语言更改:

 {% for lang in LANGUAGES %}
   <li>
      <form name="setLang{{ lang.1}}" action="/i18n/setlang/" method="POST">{% csrf_token %}
          <input name="next" type="hidden" value="http://google.com" />
         <input type="hidden" name="language" value="{{ lang.0 }}" />
        <a href="#" {% if LANGUAGE_CODE = lang.0 %} class="active" {%endif%} onclick="document.setLang{{ lang.1 }}.submit();return false;">{{ lang.0 }}</a>
      </form>
      </li>
{% endfor %}  

效果很好,问题是它不会返回“next”字段中指定的URL。我强迫它成为“google.com”只是为了看它是如何工作的,但是它一直将我重新引导回我的主页。

这是我的urls.py:

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'izolyatsia.views.home', name='home'),
# url(r'^izolyatsia/', include('izolyatsia.foo.urls')),
url(r'^$', 'showcase.views.home', name='home'),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^project/(?P<project_name>[a-zA-Z0-9_.-]+)/$', 'izolyatsia.views.project'),
(r'^post/(?P<slug>[a-zA-Z0-9_.-]+)/$', 'izolyatsia.views.post'),

#Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^wysiwyg_post/$', 'izolyatsia.views.wysiwyg_post', name='wysiwyg_post'),
url(r'', include('multiuploader.urls')),
(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^(.*)/$', 'pages.views.show_page', name='show_page'),

我不知道为什么它不起作用,也许有人可以帮助我?

提前谢谢, 米。

2 个答案:

答案 0 :(得分:1)

代码明确阻止了重定向到域外的网站 - set_language视图调用django.utils.is_safe_url(),这会检查您是不是要尝试这样做。

答案 1 :(得分:0)

请查看django doc here,您可以在settings.py文件中找到按此顺序排列的中间件:

MIDDLEWARE_CLASSES = (
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.locale.LocaleMiddleware',
   'django.middleware.common.CommonMiddleware',
)

也许这可能是你发生的事情,我想。