Download rich text:
pip install django-tinymce
A configuration
- Add settings.py file in INSTALLED_APPS: 'tinymce';
Second, the application
1, used in the admin
a, disposed in the file settings.py
TINYMCE_DEFAULT_CONFIG = {# rich text mode configuration and size
'Theme': 'advanced',
'width': 600,
'height': 400,
}
b, in addition model class file models
# Introducing rich text
from tinymce.models import HTMLField # HTMLField large text package
class Text (models.Model):
str = HTMLField () # Note: Remember to migrate the text: python manage.py makemigrations
Adding files after the migration:
python manage.py makemigrations
python manage.py migrate
c, adding management in the admin.py
.models Import Text from
admin.site.register (Text) registered in the site #
c1, create a superuser
python manage.py createsuperuser
effect:
2. Application custom view
1. Complete the configuration
2. For chestnuts:
Views.py corresponding file
# 富文本
def edit(request):
return render(request, 'myapp/edit.html')
对应的url.py文件
# 富文本
path('edit/', views.edit) # python3.x
对应的edit.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>富文本</title> <script type="text/javascript" src="/static/tiny_mce/tiny_mce.js"></script> <!--这里的/tiny_mce/tiny_mce.js是自动生成的,我们不用管--> <script type="text/javascript"> tinyMCE.init({ // 调用的方法,初始化富文本 'mode':'textareas', 'theme':'advanced', 'width':800, 'height':600, }) </script> </head> <body> <form action="/saveedit/" method="post"> <!--再写一个url匹配这个,然后再写一个views.py接收这个数据,接收后存入数据库--> <textarea name="str"> baby is girl</textarea> <input type="submit" value="提交"/> </form> </body> </html>
效果: