python - Django 。 i18N 反向 url 问题

标签 python django reverse django-i18n

此问题仅发生在生产服务器上。设置中的语言如下所示:

`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.permalink
    def get_unsubscribe_url(self):
        return ('participant-unsubscribe', [self.pk, self.unsubscribe_hash])

问题是 unsubscribe_link templatetag 返回格式为:domain/en-us/xxx/xxx 的 url,显然该 url 响应是 404。如果我将“en-us”更改为“pl”,一切正常。我找不到这个问题的根源。本地 url 已正确生成。

最佳答案

我认为您的问题与 cronjob 区域设置有关:

  1. 您可以创建(如果尚不存在)文件/etc/environment 并添加以下行:

    LANG=pl_PL.UTF-8
    
  2. 更改命令:

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()

关于python - Django 。 i18N 反向 url 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23240119/

相关文章:

python - pandas 动态列数的平均值

python - 在 ERPNext 中安装不继续?

javascript - 使用 AJAX 发送多个字段

python - 用于没有模型的对象的 Graphite 烯解析器

jquery - 模式对话框不关闭 Bootstrap

c - 尝试反转具有多个字符串的文件,但仅反转最后一个字符串

python - Pandas 线性插值按另一列分组

python - Django channel : Exception inside application: No handler for message type app_consumer. notification_message

ios - 使用自定义单元格反转 UITableView

python - 如何在 python 中反转 int?