Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

AttributeError: 'RegexURLResolver' object has no attribute '_i18n_regex_dict' #3

Closed
franckbret opened this Issue · 9 comments

4 participants

@franckbret

Hi,

I'm facing a strange problem.
Seems hard to be reproduced as it occurs only on my staging/prodution servers.
The exact same code runs fine, no errors with django server even if DEBUG is True or False...

It seems that the monkey patched I18NRegexURLResolver class can't find _i18n_regex_dict ...

Any idea ? Any Hints on how to debug case like this ?

2011-08-08 05:16:22,022 [ERROR] django.request: Internal Server Error: /fr/
    Traceback (most recent call last):
      File "/home/administrator/.virtualenvs/django1.3-001/lib/python2.6/site-packages/django/core/handlers/base.py", line 101, in get_response
        request.path_info)
      File "/home/administrator/.virtualenvs/django1.3-001/lib/python2.6/site-packages/django/core/urlresolvers.py", line 247, in resolve
        match = self.regex.search(path)
      File "/home/administrator/.virtualenvs/django1.3-001/lib/python2.6/site-packages/i18nurls/urlresolvers.py", line 74, in regex
        if language_code not in self._i18n_regex_dict:
    AttributeError: 'RegexURLResolver' object has no attribute '_i18n_regex_dict'
@svisser

I'm having the same issue. It occurs on a test server but not locally. It also goes away after a refresh, as if it's not picking up some language setting properly.

@svisser
@svisser

I was able to fix it by properly configuring the middleware. It now looks like:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'multilingual.middleware.DefaultLanguageMiddleware',
    'i18nurls.middleware.LocaleMiddleware',
    'cms.middleware.multilingual.MultilingualURLMiddleware',
    'django.middleware.common.CommonMiddleware',
    ....
@brocaar
Owner

Sorry for my late reply (I was on holiday). Can it be caused by an other package (or your own code) that imports the url resolver before it gets patched?

In that case the order of your installed apps might solve the problem? Which other packages are you using? Could you post your INSTALLED_APPS and MIDDLEWARE_CLASSES settings?

@svisser

Not sure about franckbret's situation but in my case, it was indeed due to the ordering of middleware (and using the correct middleware too, there's various Locale middleware around now). See my previous comment with my MIDDLEWARE_CLASSES.

@franckbret

I finally make it works on both local and production server. It seems that replacing
'django.middleware.locale.LocaleMiddleware' with 'i18nurls.middleware.I18NLocaleMiddleware' do the job.

I'm still confused about it cause you say in the README "Add django.middleware.locale.LocaleMiddleware to your settings.MIDDLEWARE_CLASSES (if it is not already there, make sure it comes before the CommonMiddleware)."

Do you confirm it's the good way to go or do I have miss something ?

So my final settings are :

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    #'django.middleware.locale.LocaleMiddleware',
    'i18nurls.middleware.I18NLocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.i18n",
    )

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'i18nurls',
    'i18ndemo',
)

I've made a local demo project to test all this stuff are you interested in including it in the app ?

@franckbret franckbret closed this
@brocaar
Owner

Strange, since I am patching the django.middleware.LocaleMiddleware as well (so the API will be exactly the same when you upgrade to Django 1.4, if nothing will change in meantime). What happens when you change the i18nurls middleware back to the django middleware and put the i18nurls app on top of your INSTALLED_APPS tuple?

@franckbret

Unfortunately moving i18nurls on top and activating django.middleware.LocaleMiddleware does not work. Got various AttributeError: 'RegexURLResolver' object has no attribute '_i18n_regex_dict' errors

I've just pushed my demo project here if you want to have a look

https://github.com/franckbret/django_project_i18n_demo

@max-arnold

I'm having the same issue, but it happens only with https://github.com/dcramer/django-devserver enabled.

Django Version: 1.3.1
Python Version: 2.6.6

Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'i18nurls',
 'django.contrib.admin',
 'south',
 'devserver',
 'debug_toolbar',
 'django_extensions']

Installed Middleware:
('i18nurls.middleware.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.