python - 修改包含 HTML 标记的字符串中的链接
问题描述
该模型有一个“内容”字段。更改此模型时,需要<a
在content
字段中查找符号并将行添加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)
解决方案
推荐阅读
- amazon-web-services - AWS EKS 上的 kubelet 在哪里?
- cordova - 离子应用程序不适用于 4.4 和 5.1
- powershell - “RPC 服务器不可用”错误,但脚本似乎仍然有效
- python - 在opencv中将png保存为jpg时出现问题
- python - 在 Windows 中关闭 Python 中的主程序时,通过子进程传递参数并终止子进程
- node.js - 为每个 BrowserWindow 对象创建一个新会话的能力?
- python - 在 Python 中使用任何数据类型的链表堆栈
- ios - 如果通过 Rx 数据源绑定在屏幕外加载数据,则导航栏大标题会缩小
- sql - SQL 将行从特定年份重复到当前年份
- java - 如何在 SpringBoot REST Api 中调用覆盖的 handleMethodArgumentNotValid?