我正在使用django-summernote,我需要在表单上使用自定义工具栏。我已经定制了SUMMERNOTE_CONFIG设置。但是这种特殊形式需要另外设置。所以我在表单中使用SummernoteWidget。表格是:
class CommentFormAuthenticated(forms.ModelForm):
comment = forms.CharField(widget=SummernoteWidget(attrs={'width': '20%', 'height': '500px'}),)
class Meta:
model = Comment
fields = ['comment', ]
例如,工具栏应具有以下设置:
'toolbar': [
['style', ['style']],
['style', ['bold', 'italic', 'underline', 'clear']],
['para', ['ul', 'ol', 'height']],
['insert', ['link']],
],
但是这种方式在doc中,它们在settings.py中用作SUMMERNOTE_CONFIG变量的参数之一。所以我试图以这样的形式将这些设置实现到SummernoteWidget:
class CommentFormAuthenticated(forms.ModelForm):
comment = forms.CharField(widget=SummernoteWidget(attrs={
'width': '20%',
'height': '500px',
'toolbar': [
['style', ['style']],
['style', ['bold', 'italic', 'underline', 'clear']],
['para', ['ul', 'ol', 'height']],
['insert', ['link']],
]
}),)
class Meta:
model = Comment
fields = ['comment', ]
但工具栏根本不会改变。将这些设置传递给SummernoteWidget的正确方法是什么?
答案 0 :(得分:0)
我从未使用它,但根据插件文档,工具栏配置设置在settings.py
:https://github.com/summernote/django-summernote#options
当然,这将是全球性的。我想你可以根据需要用常规方式(在javascript中)设置它们。
答案 1 :(得分:0)
实际上,您可以在settings.py
类似的事情会起作用:
SUMMERNOTE_CONFIG = {
'iframe': True,
'summernote': {
# As an example, using Summernote Air-mode
'airMode': False,
'width': '100%',
'height': '480',
'toolbar': ['bold', 'italic', 'underline'],
},
'disable_attachment': True,
}
答案 2 :(得分:0)
这有效:
models.py
class Post(models.Model):
body = models.TextField()
使用形式:
## forms.py
from django import forms
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.urls import reverse
from django.conf.settings import SUMMERNOTE_CONFIG as summernote_config
from django_summernote.widgets import SummernoteWidget
from .models Post
class SummernoteWidgetWithCustomToolbar(SummernoteWidget):
def summernote_settings(self):
summernote_settings = summernote_config.get('summernote', {}).copy()
lang = summernote_config['summernote'].get('lang')
if not lang:
lang = 'en-US'
summernote_settings.update({
'lang': lang,
'url': {
'language': static('summernote/lang/summernote-' + lang + '.min.js'),
'upload_attachment': reverse('django_summernote-upload_attachment'),
},
'toolbar': [
['style', ['style', ]],
['font', ['bold', 'italic', 'underline', 'color', ]],
['paragraph', ['paragraph', 'ol', 'ul', ]],
['misc', ['link', 'undo', 'redo', 'help', ]],
],
})
return summernote_settings
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields "__all__"
widgets = {'body': SummernoteWidgetWithCustomToolbar()}
在管理员中的用法:
## admin.py
from django import forms
from django.contrib import admin
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.urls import reverse
from django.conf.settings import SUMMERNOTE_CONFIG as summernote_config
from django_summernote.widgets import SummernoteWidget
from .models import Post
class SummernoteModelAdminWithCustomToolbar(SummernoteWidget):
def summernote_settings(self):
summernote_settings = summernote_config.get('summernote', {}).copy()
lang = summernote_config['summernote'].get('lang')
if not lang:
lang = 'en-US'
summernote_settings.update({
'lang': lang,
'url': {
'language': static('summernote/lang/summernote-' + lang + '.min.js'),
'upload_attachment': reverse('django_summernote-upload_attachment'),
},
'toolbar': [
['style', ['style', ]],
['font', ['bold', 'italic', 'underline', 'color', ]],
['paragraph', ['paragraph', 'ol', 'ul', ]],
['misc', ['link', 'undo', 'redo', 'help', ]],
],
})
return summernote_settings
class CustomPostForm(forms.ModelForm):
class Meta:
model = Post
widgets = {'body': SummernoteModelAdminWithCustomToolbar()}
fields = "__all__"
class PostAdmin(admin.ModelAdmin):
form = CustomPostForm