Django views-如何在HttpResponseRedirect中翻译信息消息

时间:2011-05-16 06:25:40

标签: django django-views

我想将HttpResponseRedirect()中的消息翻译成德语。我的网站上有一个弹出框。它显示信息。信息由视图中声明的HttpResonseRedirect提供。我必须翻译该信息..代码像这样::

如果是frmChangePassword.is_valid():                 尝试:
                    password1 = frmChangePassword.cleaned_data ['password1']                     password2 = frmChangePassword.cleaned_data ['password2']

                if password1 == password2:
                    oe = OEConnector()
                    UpdatePasswordData= oe.updateData('res.users',[request.session['user_id']],{'password':password1})
                    if UpdatePasswordData:
                        return HttpResponseRedirect('/Info?info=Password changed successfully.')
                    else:
                        return HttpResponseRedirect('/Info?info=Unable to update password. The following error occured -> ' + e.__str__())
                else:
                    return HttpResponseRedirect('/Info?info=Passwords you provided did not match.')

            except Exception as e:
                 return HttpResponseRedirect('/Info?info=Unable to update password. The following error occured -> ' + e.__str__())
    elif request.POST['action'] == "newsletter_update":
        #Check if newsletter_active checkbox has been checked
        #If not, then seeking this value will throw an error
        try:
            request.POST['newsletter_active'] 
        except:
            newsletter_status = False
        else:
            newsletter_status = True
        oe = OEConnector()
        UpdateNewsletterData= oe.updateData('res.partner.address',[request.session['partner_id']],{'newsletter_subs':newsletter_status})
        return HttpResponseRedirect('/Info?info=Newsletter updated successfully.') .

我如何翻译HttpResponseRedirect中的消息...我知道bout gettext,trans,blocktrans,inlinetrans。 提前谢谢

2 个答案:

答案 0 :(得分:0)

我认为你走错了方向,尝试通过这样的GET参数传递消息。使用django messages framework

from django.contrib import messages
if password1 == password2:
    oe = OEConnector()
    UpdatePasswordData= oe.updateData('res.users',[request.session['user_id']],{'password':password1})
    if UpdatePasswordData:
        messages.success(_('Password changed successfully.'))
    else:
        messages.error(_('Unable to update password. The following error occured -> ') + e.__str__())
else:
    messages.error(_('Passwords you provided did not match.'))
return HttpResponseRedirect('/Info')

然后你可以在模板中打印消息,给出不同CSS样式的错误和成功消息:

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

尝试使用消息框架,它将使编写时更轻松。当然,翻译消息没有问题。

答案 1 :(得分:0)

重要的是在settings.py

中的中间件中有正确的顺序
MIDDLEWARE_CLASSES = (
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.locale.LocaleMiddleware',
   'django.middleware.common.CommonMiddleware',
    )

https://docs.djangoproject.com/en/1.5/topics/i18n/translation/#how-to-create-language-files