Django 应用程序中的富文本
我想在我的 charfields 中添加一些非常基本的富文本编辑,只有粗体、斜体和自定义字体大小选择器(小-正常-大——相对于另一个模型字段设置)。起初我尝试使用 django-tinymce,但是当使用表单集时,我遇到了一些问题,tinymce 的媒体文件仅应用于集合中的第一个表单,并且我添加的 js 函数不起作用。也许我无论如何都需要一个用于这个小-正常-大字体大小选择器的自定义小部件(或者将其作为模型字段执行)。
在这种情况下,是否建议用户使用 TinyMCE 并尝试对其进行自定义(如果是,如何自定义),或者是否有更直接的方法来使用我已经创建的表单?
这是我的表单中的内容(动态更改文本区域大小,并调用模板中的 js 函数来限制字符):
text=forms.CharField(max_length = 1000, widget=forms.widgets.Textarea())
def __init__(self, *args, **kwargs):
size = kwargs.pop('size')
maxChars = kwargs.pop('maxChars')
super(MyForm, self).__init__(*args, **kwargs)
self.fields['text'].widget.attrs['onkeypress'] = 'return textCounter(this, this.form.counter, %d);' % maxChars
self.fields['text'].widget.attrs['rows'] = size
self.fields['text'].widget.attrs['cols'] = '40'
I would like to add some very basic rich text editing in my charfields, only bold, italic and a custom fontsize selector (small-normal-large -- relative to another model field setting). At first I tried to use django-tinymce, but when using a formset I had some problems that the media files for tinymce only applied to the first form in the set, and my added js function doesnt work. Maybe I need a custom widget for this small-normal-large fontsize selector anyway (or do this as a model field).
Is it recommendable to user TinyMCE and try to customize it in this case (if so, how), or is there a more straightforward way to go using the form I've already created?
Here's what I have in my form (dynamically changing the textarea size, and calling js function in the template to limit chars):
text=forms.CharField(max_length = 1000, widget=forms.widgets.Textarea())
def __init__(self, *args, **kwargs):
size = kwargs.pop('size')
maxChars = kwargs.pop('maxChars')
super(MyForm, self).__init__(*args, **kwargs)
self.fields['text'].widget.attrs['onkeypress'] = 'return textCounter(this, this.form.counter, %d);' % maxChars
self.fields['text'].widget.attrs['rows'] = size
self.fields['text'].widget.attrs['cols'] = '40'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 ckeditor,无需任何 django-app 即可启用它。另一种选择是(非常)基本的发光编辑器,请参阅 BBC 的发光主页。
就我个人而言,我不会在您的表单中包含该 javascript 顺便说一句,而是将其包含在模板中。
You can use ckeditor, which you can enable without any django-app. An other alternative is the (very) basic glow editor, see BBC's glow homepage.
Personnally I would not include that javascript in your form btw, but include it in the template.