默认语言通过设置在测试期间不被尊重 [英] Default language via settings not respected during testing

查看:120
本文介绍了默认语言通过设置在测试期间不被尊重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Django 1.3,Python 2.6

Using Django 1.3, Python 2.6

有一个特别奇怪的问题跟踪与
国际化相关,RequestFactory vs. TestClient用于测试
意见。

Having a particularly weird problem to track down related to internationalization, and RequestFactory vs. TestClient for testing views.

如果我运行:

./manage.py test 

所有测试运行(包括有问题的)并成功传递。如果我运行:

All tests run (including the problematic ones) and pass successfully. If I run:

./manage.py test <appname> 

应用程序的测试将失败,抛出使用该语言的
模板的模板渲染异常代码,因为语言django
认为请求是要求不是我们列在
settings.LANGUAGES中的语言。 (在这种情况下,一直是'en-us',我们支持的壁橱匹配语言是'en')

The app's tests will fail, throwing a template rendering exception for templates that use the language code because the language django thinks the request is asking for is not a language we've listed in settings.LANGUAGES. (In this case it has always been 'en-us', the closet matching language we support being 'en')

这是一个测试失败的例子:

Here's an example of a test that will fail:

class TemplateServingTestCase(TestCase):
    def setUp(self):
        self.app_dir      = os.path.abspath(os.path.dirname(__file__))
        self.gallery_root = os.path.join(self.app_dir, 'test_gallery')
        self.gallery_url  = '/'
        self.request      = RequestFactory().get('/')

    def test_404_invalid_category(self):
        self.assertRaises(Http404, gallery_page,
            self.request,
            'bad-category',
            self.gallery_root,
            self.gallery_url
        )

如果django的TestClient用于向调用特定视图的URL发出
请求,则不会发生此问题。但是,如果使用RequestFactory的get或put
方法简单调用相同的
视图,则会抛出上面的错误。

This problem will not occur if django's TestClient is used to make a request to a url that calls a particular view. However if that same view is simply called with the result of RequestFactory's get or put methods, it will throw the error above.

在使用RequestFactory方法时,似乎没有尊重
设置文件。我在这里缺少一些简单的

It appears as though when using the RequestFactory method, the settings file is not being respected. Am I missing something simple here?

适用的区域设置

LANGUAGE_CODE = 'en'
LANGUAGES = (
    ('en', 'English'),
    ('de', 'Deutsch'),
    ('es', 'Espanol'),
    ('fr', 'Francaise'),
    ('it', 'Italiano'),
    ('pt-br', 'Portugues (Brasil)'),
)

活动中间件

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'services.middleware.LegacyIntegrationMiddleware',
)


推荐答案

有两种可能性:

a)您在设置中有'django.middleware.locale.LocaleMiddleware' .MIDDLEWARE_CL ASSES。

a) You have 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE_CLASSES.

在这种情况下,客户端使用 settings.LANGUAGE_CODE

In this case, the client use settings.LANGUAGE_CODE.

b)你没有

在这种情况下,您应该在test.py模块中设置一些这样的语言:

In that case, you should set the language like that somewhere in your tests.py module:

from django.utils.translation import activate
...
activate('fr-fr')

https://code.djangoproject.com/ticket / 15143

这篇关于默认语言通过设置在测试期间不被尊重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆