Django 应用程序中的富文本

发布于 2024-10-05 12:08:23 字数 827 浏览 0 评论 0原文

我想在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2024-10-12 12:08:24

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文