for AuthenticationMiddleware we have already this systemCheck admin.E410:
ERRORS: (admin.E410)
'django.contrib.sessions.middleware.SessionMiddleware' must be in
MIDDLEWARE in order to use the admin application.
HINT: Insert
'django.contrib.sessions.middleware.SessionMiddleware' before
'django.contrib.auth.middleware.AuthenticationMiddleware'.
But we don't have those check for LocaleMiddleware
--
Ticket URL: <https://code.djangoproject.com/ticket/34030>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => Jay Patel
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:1>
Comment (by Jay Patel):
Check for LocaleMiddleware to come after SessionMiddleware can be done by
`if not hasattr(request, "session")` in `def process_request` of class
LocalMiddleware but how to check that it should come before
CommonMiddleware? or am I thinking in a complete wrong direction.
--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:2>
Comment (by Mark Walker):
You can check the position in the middleware of a given path;
{{{
#!bash
>>> MIDDLEWARE.index('django.middleware.locale.LocaleMiddleware')
12
>>>
}}}
I can implement this - it's just a matter of where to run the check. When
the settings are configured there are some checks depending on `USE_L10N`,
so perhaps this check could hang off `USE_I18N` being set?
--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:3>
Comment (by Maxim Danilov):
Replying to [comment:2 Jay Patel]:
> Check for LocaleMiddleware to come after SessionMiddleware can be done
by `if not hasattr(request, "session")` in `def process_request` of class
LocalMiddleware but how to check that it should come before
CommonMiddleware?
Hi, Jay. On my talk on django con i use this trick:
{{{
# settings.py
if USE_I18N:
MIDDLEWARE.insert(
next((idx for idx, _mw in enumerate(MIDDLEWARE, 1) if
_mw.endswith('SessionMiddleware')), 0),
'django.middleware.locale.LocaleMiddleware'
)
}}}
i check settings USE_I18N, and only after that i start to do something
with SessionMiddleware. I add LocaleMiddleware directly after
SessionMiddleware
otherwise it should be exception, but i skip it.
probably you can use it to check idx_of_SessionMiddleware <
idx_of_LocaleMiddleware < idx_of_commonMiddlevare.
It is don't matter, where to check, to check settings you don't need the
request.
--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:4>
Comment (by Jay Patel):
Replying to [comment:4 Maxim Danilov]:
> Replying to [comment:2 Jay Patel]:
> > Check for LocaleMiddleware to come after SessionMiddleware can be done
by `if not hasattr(request, "session")` in `def process_request` of class
LocalMiddleware but how to check that it should come before
CommonMiddleware?
>
> Hi, Jay. On my talk on django con i use this trick:
>
>
> {{{
> # settings.py
>
> if USE_I18N:
> MIDDLEWARE.insert(
> next((idx for idx, _mw in enumerate(MIDDLEWARE, 1) if
_mw.endswith('SessionMiddleware')), 0),
> 'django.middleware.locale.LocaleMiddleware'
> )
> }}}
>
> i check settings USE_I18N, and only after that i start to do something
with SessionMiddleware. I add LocaleMiddleware directly after
SessionMiddleware
> otherwise it should be exception, but i skip it.
>
> probably you can use it to check idx_of_SessionMiddleware <
idx_of_LocaleMiddleware < idx_of_commonMiddlevare.
>
> It is don't matter, where to check, to check settings you don't need the
request.
Got it! Maxim, now where should I add these checks? should it be another
translation configuration check or something else?
--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:5>
* status: assigned => closed
* resolution: => wontfix
Comment:
TBH I don't think this is worth the complexity.
`LocaleMiddleware` is not required, and it's not in the default template,
so someone has to both go and add it, and add it in the wrong place, which
given they got through the i18n docs, I'm not sure is likely.
Note also that whilst the `hint` for `admin.E410` says to ''insert between
…'', we don't actually check that — just that `SessionMiddleware` is
installed.
I think trying to check exact/relative positions would be an unnecessary
burden.
I hope that makes sense.
--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:6>