1

What I want to do: I'm trying to run tests with unittest for functions in my views.

Outcome: I'm getting the following error:

....env/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 189, in _fetch "The translation infrastructure cannot be initialized before the " django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.

Imports used:

import unittest
from django.test import Client
from django.core.wsgi import get_wsgi_application
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
import sys
sys.path.append('../../mysite/')
from mysite.settings import *
from views import *
application = get_wsgi_application()

As you can see I tried this answer with no success: appregistrynotready-the-translation-infrastructure-cannot-be-initialized

I followed this one too: upgrading-to-django-1-7-getting-appregistrynotready-for-translation-infrastruct

Imports I found with ugettext & ugettext_lazy:

from django.utils.translation import ungettext, ugettext_lazy as _
from django.utils.translation import ungettext, ugettext, ugettext_lazy as _
from django.utils.translation import ugettext_lazy as _

I changed them to

from django.utils.translation import ugettext_lazy as _

But it didn't work either

Some code I found with ugettext

return ugettext('%(number)d %(type)s') % {'number': delta.seconds, 100  'type': count(delta.seconds)}

I was wondering if there could be a problem with this.

I found it worked if I removed this lines from authentication/models.py:

last_pass_change = models.DateTimeField(_("last_pass_change"), default=datetime.datetime.now())
last_failed_login = models.DateTimeField(_("last_failed_login"), default=datetime.datetime.now())

But I don't know how to fix it

Community
  • 1
  • 1
sheilapbi
  • 325
  • 1
  • 15
  • Why are you not using the [existing test infrastructure](https://docs.djangoproject.com/en/1.10/topics/testing/overview/), which also provides you with a [dummy client for testing views](https://docs.djangoproject.com/en/1.10/topics/testing/tools/#test-client)? – dhke Sep 06 '16 at 15:11
  • `default=datetime.datetime.now()` <= This sets the default value to the date and time when `models.py` is loaded. You probably wanted to omit the brackets. And you also might want to read about [timezones and datetimes in django](https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#naive-and-aware-datetime-objects) – dhke Sep 06 '16 at 15:14

2 Answers2

3

Finally I solved it changing:

from django.utils.translation import gettext as _

to:

from django.utils.translation import ugettext_lazy as _
sheilapbi
  • 325
  • 1
  • 15
0

Initialize the WSGI application before you import from views and settings.

application = get_wsgi_application()    
from mysite.settings import *
from views import *
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75