Code/Django

django wagtail summernote TypeError: clean() got an unexpected keyword argument 'styles'

하말 ⍺ 2024. 8. 1. 16:01
반응형

 

TypeError: clean() got an unexpected keyword argument 'styles'


edit django_summernote/fields.py

pip install tinycss2
vi .venv/lib/python3.12/site-packages/django_summernote/fields.py
# fields.py
# bleach.clean 함수가 styles 인수를 지원하지 않는 것 같습니다. bleach.clean 함수의 인수 목록을 확인하고, styles 인수를 제거하거나 다른 방법으로 스타일을 처리해야 합니다.
from django.db import models
from django.forms import fields

import bleach
from bleach.css_sanitizer import CSSSanitizer
from django_summernote.settings import ALLOWED_TAGS, ATTRIBUTES
from django_summernote.widgets import SummernoteWidget

# code based on https://github.com/shaunsephton/django-ckeditor

# CSSSanitizer 설정
css_sanitizer = CSSSanitizer(allowed_css_properties=['color', 'font-weight', 'font-style', 'text-decoration'])

class SummernoteTextFormField(fields.CharField):
    def __init__(self, *args, **kwargs):
        kwargs.update({'widget': SummernoteWidget()})
        super().__init__(*args, **kwargs)

    def to_python(self, value):
        value = super().to_python(value)
        return bleach.clean(
            value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES, css_sanitizer=css_sanitizer)

class SummernoteTextField(models.TextField):
    def formfield(self, **kwargs):
        kwargs.update({'form_class': SummernoteTextFormField})
        return super().formfield(**kwargs)

    def to_python(self, value):
        value = super().to_python(value)
        return bleach.clean(
            value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES, css_sanitizer=css_sanitizer)

    def from_db_value(self, value, expression, connection):
        if value is None:
            return value
        return bleach.clean(
            value, tags=ALLOWED_TAGS, attributes=ATTRIBUTES, css_sanitizer=css_sanitizer)