Django集成富文本編輯器summernote的實(shí)現(xiàn)步驟
提到Django的富文本編輯器,大家一定會(huì)想到ckeditor和tinyMCE。其實(shí)還是有一個(gè)富文本編輯器同樣優(yōu)秀,它就是summernote,個(gè)人認(rèn)為功能上不遜于ckeditor,比tinyMCE更強(qiáng)大。Summernote 是一個(gè)簡(jiǎn)單靈活的所見(jiàn)即所得的 HTML 富文本編輯器,基于 jQuery 和 Bootstrap 構(gòu)建,支持圖片上傳,提供了大量可定制的選項(xiàng)。
展示效果如下所示:
首先通過(guò)pip安裝django-summernote,建議安裝在Django項(xiàng)目所在的虛擬環(huán)境里。如果你要上傳圖片,還需要安裝pillow這個(gè)圖片庫(kù)。
pip install django-summernotepip install pillow # 上傳圖片時(shí)需要
接著將其加入到INSTALLED_APPS里去,如下所示:
INSTALLED_APPS = [ ... ’django_summernote’, # 注意下劃線(xiàn)]
然后將django_summernote.urls 加入到項(xiàng)目的 urls.py
from django.urls import include# ...urlpatterns = [ ... path(’summernote/’, include(’django_summernote.urls’)), ...]
如果你需要上傳圖片,還需要在settings.py中設(shè)置MEDIA相關(guān)選項(xiàng),如下所示。如果你Django的版本是3.x的,你還需設(shè)置X_FRAME_OPTIONS選項(xiàng)。
MEDIA_URL = ’/media/’MEDIA_ROOT = os.path.join(BASE_DIR, ’media/’)# Django 3.X用戶(hù)還需增加如下配置X_FRAME_OPTIONS = ’SAMEORIGIN’
如果你在本地開(kāi)發(fā)測(cè)試環(huán)境debug=True, 你還需要使用django自帶static靜態(tài)文件服務(wù)器才能正確顯示上傳的圖片。修改項(xiàng)目的urls.py, 添加如下代碼:
from django.conf import settingsfrom django.conf.urls.static import staticif settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)第二步 使用django-summernote
你可以在Django自帶管理后臺(tái)admin中使用django-summernote, 也可以在自己的表單中使用django-summernote。
admin中使用
from django_summernote.admin import SummernoteModelAdminfrom .models import Postclass PostAdmin(SummernoteModelAdmin): summernote_fields = (’content’,)admin.site.register(Post, PostAdmin)
展示效果如下所示:
表單中使用
如果你使用普通表單,只需要設(shè)置富文本顯示字段的widget即可,如下所示:
from django_summernote.widgets import SummernoteWidget, SummernoteInplaceWidget# Apply summernote to specific fields.class PostForm(forms.Form): content = forms.CharField(widget=SummernoteWidget()) # instead of forms.Textarea# 如果你已使用django-crispy-forms, 請(qǐng)使用class PostForm(forms.Form): content = forms.CharField(widget=SummernoteInplaceWidget())
如果你使用ModelForm, 可以通過(guò)如下方式設(shè)置widget。
class PostForm(forms.ModelForm): class Meta:model = Postwidgets = { ’content’: SummernoteWidget(),}
注意:通過(guò)表單提交的內(nèi)容都是帶html標(biāo)簽的,需正確顯示文本,需要使用safe模板標(biāo)簽。
{{ content|safe }}由于SummernoteWidget對(duì)用戶(hù)提交的數(shù)據(jù)不做任何轉(zhuǎn)義,所以存在外部用戶(hù)通過(guò)表單注入惡意腳本的風(fēng)險(xiǎn),小編并不建議使用。在表單中使用django-summernote更好的方式是使用SummernoteTextFormField和SummernoteTextField,它們會(huì)對(duì)所有有害的標(biāo)簽進(jìn)行轉(zhuǎn)義。使用方式如下所示:
第三步 測(cè)試效果Djangos-summernote不僅可以上傳圖片,還可以嵌入視頻哦,親測(cè)成功!
常用設(shè)置選項(xiàng)如下所示,可以滿(mǎn)足大部分項(xiàng)目需求,可以直接copy使用。
SUMMERNOTE_CONFIG = { ’iframe’: True, # 如果你本身已使用Bootstrap/jQuery主題 # ’iframe’: False, ’summernote’: {# As an example, using Summernote Air-mode’airMode’: False,# 編輯窗口 size’width’: ’100%’,’height’: ’450’,# 語(yǔ)言設(shè)置’lang’: None,# 工具欄圖標(biāo)# https://summernote.org/deep-dive/#custom-toolbar-popover’toolbar’: [ [’style’, [’style’,]], [’font’, [’bold’, ’underline’, ’clear’]], [’fontname’, [’fontname’]], [’fontsize’, [’fontsize’]], [’color’, [’color’]], [’para’, [’ul’, ’ol’, ’paragraph’]], [’table’, [’table’]], [’insert’, [’link’, ’picture’, ’video’]], [’view’, [’redo’, ’undo’, ’fullscreen’, ’codeview’,]],], }, # 上傳圖片需要用戶(hù)先登錄. ’attachment_require_authentication’: True, # Set `upload_to` function for attachments. # ’attachment_upload_to’: my_custom_upload_to_func(), # Set custom storage class for attachments. # ’attachment_storage_class’: ’my.custom.storage.class.name’,# You can completely disable the attachment feature. ’disable_attachment’: False,# Set to `True` to return attachment paths in absolute URIs. ’attachment_absolute_uri’: False,# test_func in summernote upload view. (Allow upload images only when user passes the test) # https://docs.djangoproject.com/en/2.2/topics/auth/default/#django.contrib.auth.mixins.UserPassesTestMixin # ``` # def example_test_func(request): # return request.user.groups.filter(name=’group_name’).exists() # ``` # ’test_func_upload_view’: example_test_func,# 懶加載 ’lazy’: True,}
以上就是Django集成富文本編輯器summernote的實(shí)現(xiàn)步驟的詳細(xì)內(nèi)容,更多關(guān)于Django集成富文本編輯器summernote的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python2.6版本pip安裝步驟解析2. python公司內(nèi)項(xiàng)目對(duì)接釘釘審批流程的實(shí)現(xiàn)3. python中Ansible模塊的Playbook的具體使用4. Python自動(dòng)化之定位方法大殺器xpath5. Python本地及虛擬解釋器配置過(guò)程解析6. Python 利用flask搭建一個(gè)共享服務(wù)器的步驟7. 基于python實(shí)現(xiàn)matlab filter函數(shù)過(guò)程詳解8. Python中Anaconda3 安裝gdal庫(kù)的方法9. python自動(dòng)化測(cè)試三部曲之request+django實(shí)現(xiàn)接口測(cè)試10. Python importlib模塊重載使用方法詳解
