【问题标题】:How to remove Ck editor tag in django rest framework如何在 django rest 框架中删除 Ck 编辑器标签
【发布时间】:2019-06-18 13:16:36
【问题描述】:

我正在使用 Django 休息框架。我用于 Web 的 CkEditor 标签出现在 API 部分。如何从 API 中删除这些标签

我之前没用过 Ckeditor,我用的是 Django TextField 字段

模型.py

class Lesson(models.Model):
    ...
    lesson_content = RichTextField(
        verbose_name=_('Ders İçeriği'),
        blank=True
    )
    ...

序列化器.py

class LessonSerializer(serializers.ModelSerializer):
    class Meta:
        model = Lesson
        fields = (...,'lesson_content',...)

输出(Json 数据)

"id":1,
"user":2,
"lesson_name":"Microprocessors","lesson_content":"/<p>Merkezi .</p>",
"lesson_notes":"<p>&nbsp;</p>\r\n\r\n<p>Yazıcı, R., 1998, Mikrobilgisayar Donanım ve Yazılımı, KT&Uuml; Yayınları, Trabzon, 345 s.</p>\r\n\r\n<p>Brey, B., B., 1984, Microprocessor/Hardware Interfacing and Applications, Merrill, 414 p.</p>\r\n\r\n<p>Leventhal, L., A., 1979, Z80 Assebly Language Programming, Osborne/McGraw-Hill, 612 p.</p>\r\n\r\n<p>Uffenbeck, J., 1985, Microcomputers and Microprocessors: The 8080, 8085, and Z80 Programming, Interfacing, and Troubleshooting, Prentice-Hall, 670 p.</p>\r\n\r\n<p>&nbsp;</p>"

<p> & nbsp; </p> \ r \ n \ r \ n <p> I don't want the tags to appear

【问题讨论】:

  • 你可以试试这个 -> stackoverflow.com/questions/1977791/…
  • 必需标签不显示在 html 页面中。标签显示在 api 页面和移动设备上。我希望它不会出现在手机上。我希望删除 json 数据中的标签@adnan kaya
  • 你能试试这个解决方案吗? -> stackoverflow.com/questions/880188/…
  • @adnankaya 不幸的是它没有用。不适用于休息框架

标签: django python-3.x django-rest-framework ckeditor


【解决方案1】:

您可以尝试在您的序列化程序类中覆盖to_representation 方法并使用strip_tags

from django.utils.html import strip_tags


class LessonSerializer(serializers.ModelSerializer):
    class Meta:
        model = Lesson
        fields = (...,'lesson_content',...)

    def to_representation(self, instance):
        data = super().to_representation(instance)
        data['lesson_content'] = strip_tags(instance.lesson_content)
        return data

【讨论】:

  • 谢谢。如果我们想指定两个字段,我该怎么办@wencakisa
猜你喜欢
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 2019-04-13
相关资源
最近更新 更多