Issue #19 open

Require explicit monkeypatching instead of relying on being first to load

Patryk Zawadzki
created an issue

If any of the other {{{INSTALLED_APPS}}} imports a model in its {{{}}} file, that model (and other models coming from the same app) will be loaded before localeurl has a chance to patch {{{resolve()}}}. The magic breaks and some models return localized URLs for {{{get_absolute_url()}}} while others return the unlocalized ones.

A solution that always works is to always explicitly call:

{{{ from localeurl.models import patch_reverse patch_reverse() }}}

...on top of your {{{}}}.

However currently force-importing {{{localeurl.models}}} has the side effect of reloading the whole settings file twice due to a circular dependency between the two (localeurl imports settings to check {{{USE_I18N}}}).

This is why I propose to: # move the {{{patch_reverse}}} to the main module level ({{{}}}) # drop the call to {{{patch_reverse}}} and possibly replace it with a warning if the function is still unpatched once the control flow reaches {{{}}} # document the need to call the patcher directly

Comments (7)

  1. Carl Meyer repo owner
    • changed status to open

    The explicit patching should happen in (or whatever ROOT_URLCONF points to), not Importing anything in is a bad pattern. Since this is url-related, is a sensible place for it anyway.

    Otherwise, this seems like a sensible proposal.

  2. Bojan Mihelac

    I have just been bitten by similar problem. For some reason in a local development server URLs where prefixed properly, while on the production server some were and some other were not. Production is apache+modwsgi. It would be too painful to debug this in production and try to find exactly where and why the issue appear.

    Explicit patching on top of as noted above resolved this issue, so I guess it would be good idea to document it.

  3. hejsan

    I just spent half a day trying to workaround this. Silly me for not realizing this was a common error. This should definitely be mentioned in the installation docs. I fixed it by putting this at the top of my

    from localeurl.models import patch_reverse

    Thanks patrys!

  4. Log in to comment