【问题标题】:django-taggit doesn't support smart quotesdjango-taggit 不支持智能引号
【发布时间】:2022-11-14 04:20:49
【问题描述】:

当提交带有 Django-taggit 标签字段的表单时,包含多个单词的标签通过将标签用引号引起来保持在一起/不被空格分隔,例如“foo bar”应该创建 [“foo bar”] 的标签。但是,在支持智能引号的设备上,taggit 不会识别智能引号并将提交拆分为 [bar]、“foo”。

问题在于 utils 中的 _parse_tags 仅检查 '"' (unicode 34)。而在我的 iPhone 上,智能引号默认为 unicode 8220-1。当然,用户可以通过设置 > 常规 > 键盘禁用智能标点符号并关闭可以解决问题的智能标点符号。但是,要求用户解决问题似乎并不是真正的解决方案。

智能标点符号也不能在 clean_tags 中处理,因为 _parse_tags 在字段被清理后被上游调用。

【问题讨论】:

    标签: django-taggit


    【解决方案1】:

    我通过在调用字段 clean 之前更新表单数据并将任何智能引号映射到正常的 unicode 34 引号来处理这个问题。

    from collections import OrderedDict.
    
    class TaggitForm(forms.Form):
    # the basic ascii quote in unicode is 34, ex: '"'
    _quote_map = OrderedDict({
        # in unicode since it would be hard to tell what characters are being used visually
        # old: new
    
        # apple (at least iPhone) smart quotes
        8220: 34,
        8221: 34,
        8222: 34,
    })
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
    
        # get a mutable copy of QueryDict
        self.data = self.data.copy()
    
        if 'tags' in self.data:
            tags = self.data['tags']
            for k, v in self._quote_map.items():
                tags = tags.replace(chr(k), chr(v))
    
            self.data['tags'] = tags
    

    到目前为止,这似乎解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 2020-06-23
      • 2010-10-14
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多