Django 第三方引用富文本编辑器6.1
生活随笔
收集整理的這篇文章主要介紹了
Django 第三方引用富文本编辑器6.1
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 借助富文本編輯器,管理員能夠編輯出來一個包含html的頁面,從而頁面的顯示效果,可以由管理員定義,而不用完全依賴于前期開發(fā)人員
- 此處以tinymce為例,其它富文本編輯器的使用可以自行學習
- 使用編輯器的顯示效果為:
?
下載安裝
- 在網站pypi網站搜索并下載"django-tinymce-2.4.0"
- 解壓
tar zxvf django-tinymce-2.4.0.tar.gz
- 進入解壓后的目錄,工作在虛擬環(huán)境,安裝
python setup.py install
應用到項目中
- 在settings.py中為INSTALLED_APPS添加編輯器應用
INSTALLED_APPS = (...'tinymce',
)
- 在settings.py中添加編輯配置項
TINYMCE_DEFAULT_CONFIG = {'theme': 'advanced','width': 600,'height': 400,
}
- 在根urls.py中配置
urlpatterns = [...url(r'^tinymce/', include('tinymce.urls')),
]
- 在應用中定義模型的屬性
from django.db import models
from tinymce.models import HTMLFieldclass HeroInfo(models.Model):...hcontent = HTMLField()
- 在后臺管理界面中,就會顯示為富文本編輯器,而不是多行文本框
自定義使用
- 定義視圖editor,用于顯示編輯器并完成提交
def editor(request):return render(request, 'other/editor.html')
- 配置url
urlpatterns = [...url(r'^editor/$', views.editor, name='editor'),
]
- 創(chuàng)建模板editor.html
<!DOCTYPE html>
<html>
<head><title></title><script type="text/javascript" src='/static/tiny_mce/tiny_mce.js'></script><script type="text/javascript">tinyMCE.init({'mode':'textareas','theme':'advanced','width':400,'height':100});</script>
</head>
<body>
<form method="post" action="/content/"><input type="text" name="hname"><br><textarea name='hcontent'>哈哈,這是啥呀</textarea><br><input type="submit" value="提交">
</form>
</body>
</html>
- 定義視圖content,接收請求,并更新heroinfo對象
def content(request):hname = request.POST['hname']hcontent = request.POST['hcontent']heroinfo = HeroInfo.objects.get(pk=1)heroinfo.hname = hnameheroinfo.hcontent = hcontentheroinfo.save()return render(request, 'other/content.html', {'hero': heroinfo})
- 添加url項
urlpatterns = [...url(r'^content/$', views.content, name='content'),
]
- 定義模板content.html
<!DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
姓名:{{hero.hname}}
<hr>
{%autoescape off%}
{{hero.hcontent}}
{%endautoescape%}
</body>
</html>
總結
以上是生活随笔為你收集整理的Django 第三方引用富文本编辑器6.1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django 分页和使用Ajax5.3
- 下一篇: Django 缓存6.2