首页 > 解决方案 > 修改包含 HTML 标记的字符串中的链接

问题描述

该模型有一个“内容”字段。更改此模型时,需要<acontent字段中查找符号并将行添加rel="nofollow"到其中(就像在标记中一样)。我正在尝试使用 models.py 文件中的函数来执行此操作。告诉我该怎么做?

此模型的保存功能:

def save(self, *args, **kwargs):
    if (self.original_price
            and not isinstance(self.original_price, Decimal)):
        self.original_price = Decimal(self.original_price)

    if self.parent_id is not None and self.parent_id == 0:
        self.parent = None

    # Если товар не доступен для заказа, то amount_europe и
    # amount_moscow считаем что не заданы
    if self.amount_free == 0:
        self.amount_europe = None
        self.amount_moscow = None

    if self._state.adding:
        self.sort_order = 0

    if self.content:
        self.content = self.content.replace('\n', '<br>')
        if '<a ' in self.content or '<iframe ' in self.content:
            self.content = re.sub(r'<.*?>', '', self.content,
                                  flags=re.IGNORECASE)

    # Устанавливаем цену для сортировки
    self.price_ordering = self.get_price()

    super().save(*args, **kwargs)

标签: python

解决方案


推荐阅读