Search code examples
pythondjangoreversedjango-i18n

Django. i18N reverse url Issue


This issue occurs only on production server. Languages in settings looks like this:

`LANGUAGE_CODE = 'pl'
LANGUAGES = [
    ('pl', gettext('PL')),
    ('en', gettext('EN')),
]`

@register.simple_tag(takes_context=True)
def unsubscribe_link(context, href):
    domain = 'http://' + Site.objects.get_current().domain
    a = '<a href="%s" target="_self">%s</a>'
    if context.get('preview'):
        return a % ('#', href)
    return a % (domain + context['participant'].get_unsubscribe_url(), href)

models.participant :

    @models.permalink
    def get_unsubscribe_url(self):
        return ('participant-unsubscribe', [self.pk, self.unsubscribe_hash])

The issue is that unsubscribe_link templatetag return url in format: domain/en-us/xxx/xxx, and obviously that url response is 404. If I change "en-us" to "pl" all works fine. I can't find origin of this problem. Locally urls are generated properly.


Solution

  • I think your problem is connected with cronjob locale settings:

    1. you can do instead is create (if not already present) the file /etc/environment and add the following line:

      LANG=pl_PL.UTF-8
      
    2. change your command:

    class Command(BaseCommand):
    
        can_import_settings = True
    
        def handle(self, *args, **options):
    
            # Activate a fixed locale, e.g. Polish
            translation.activate('pl')
    
            # Or you can activate the LANGUAGE_CODE # chosen in the settings:
            #
            #from django.conf import settings
            #translation.activate(settings.LANGUAGE_CODE)
    
            # Your command logic here
            # ...
    
            translation.deactivate()