django-tinymce icon indicating copy to clipboard operation
django-tinymce copied to clipboard

Is it possible to add video_template_callback function code?

Open twkrol opened this issue 4 years ago • 1 comments

Hi I need to remove the default width & height media attributes (they're added automatically when inserting video), so found the only way by overwrite the video_template_callback. But I can't find a way to provide video_template_callback function, neither via TINYMCE_DEFAULT_CONFIG or mce_attrs.

Any idea how can I change media redering function?

twkrol avatar Aug 24 '21 11:08 twkrol

To remove the default width and height media attributes when inserting a video using django-tinymce, you can create a video_template_callback function in your Django project's settings.py file. Here is an example implementation:

# settings.py
TINYMCE_DEFAULT_CONFIG = {
    'plugins': 'media',
    'media_dimensions': False,
    'video_template_callback': 'myapp.tinymce_video_template_callback',
}

def tinymce_video_template_callback(file, options):
    """
    Custom video template callback to remove default width and height attributes.
    """
    attrs = ''
    for key, value in options.items():
        if key == 'src':
            value = file.url
        attrs += ' {}="{}"'.format(key, value)
    return '<video{}></video>'.format(attrs)

In this example, we set the media_dimensions option to False to disable the default width and height attributes for all media types. Then, we define a custom tinymce_video_template_callback function that generates the HTML for the video element without including the width and height attributes.

Note that you will need to replace myapp with the name of your Django app in the video_template_callback option.


Remove the default width and height attributes from the iframe tag when inserting a video in django-tinymce editor:

from tinymce import HTMLField
from django import forms

class CustomTinyMCEWidget(forms.Textarea):
    def init(self, args, **kwargs):
        super(CustomTinyMCEWidget, self).init(args, **kwargs)
        self.attrs.update({'class': 'tinymce'})

class VideoForm(forms.ModelForm):
    description = HTMLField(widget=CustomTinyMCEWidget())

    class Meta:
        model = Video
        fields = ('title', 'embed_code','description')

class VideoInline(admin.TabularInline):
    model = Video
    fields = ('title', 'embed_code', 'description')
    form = VideoForm
    extra = 0

    def video_template_callback(self, data):
        """
        Remove default media attributes from iframe tag
        """
        template = Template('<iframe src="{{src}}" width="{{width}}" height="{{height}}" frameborder="0" allowfullscreen></iframe>')
        width = data['width'].replace('px', '').strip()
        height = data['height'].replace('px', '').strip()
        src = data['src']
        return template.render(Context({'src': src, 'width': width, 'height': height}))

some1ataplace avatar Mar 24 '23 17:03 some1ataplace