You can enhance TinyMCE's link and image dialogs by providing a predefined list of options via link_list and image_list in mce_attrs. These options must point to a URL that returns a JSON array of objects.
JSON Format Requirement:
Each object in the array must contain title and value fields:
[
{"title": "My Page 1", "value": "https://example.com/page1"},
{"title": "My Page 2", "value": "https://example.com/page2"}
]
Implementation Example:
To use a predefined link list in a form field:
from django import forms
from django.urls import reverse
from tinymce.widgets import TinyMCE
class SomeForm(forms.Form):
somefield = forms.CharField(
widget=TinyMCE(mce_attrs={'link_list': reverse('someviewname')})
)
And the corresponding view:
from django.http import JsonResponse
def someview(request):
# ... fetch objects ...
link_list = [{"title": str(obj), "value": obj.get_absolute_url()} for obj in objects]
return JsonResponse(link_list, safe=False)
from django import forms
from django.urls import reverse
from tinymce.widgets import TinyMCE
class SomeForm(forms.Form):
somefield = forms.CharField(
widget=TinyMCE(mce_attrs={'link_list': reverse('someviewname')})
)