Django—富文本编辑器

  • 配置


    # 安装  pip install django-tinymce
    配置
    
    (1) 配置settings文件
    在INSTALL_APPS 添加如下代码
    
    
    INSTALLED_APPS = [
     ...
     'App',
     'tinymce',
    ]#富⽂本编辑器的配置
    
    
    在settings.py下添加如下代码
    TINYMCE_DEFAULT_CONFIG = {
     'theme':'advanced',
     'width':600,
     'height':400
    }
  • 实现


    # views.py
    
    def index(req):
        if req.method == 'GET':
             return render(req,'index.html')
         if req.method == 'POST':
         # print(req.POST)
     
    Posts(title=req.POST.get('title'),content=req.POST.get('content')).save()
        return HttpResponse('index')
    <!DOCTYPE html>
    <html lang="en">
    <head>
     <meta charset="UTF-8">
     <title>Title</title>
     <script src="/static/tiny_mce/tiny_mce.js"></script>
     <script>
     tinyMCE.init({
     'mode':'textareas',
     'width':800,
     'height':600,
     })
     </script>
    </head>
    <body>
    <form action="/" method="POST">
     {% csrf_token %}
     <p>标题 <input type="text" name="title" placeholder="请输⼊标题"
    maxlength="20" required></p>
     <textarea name="content" id="" cols="30" rows="10"></textarea>
     <input type="submit">
    </form>
    </body>
    </html>
发布了199 篇原创文章 · 获赞 6 · 访问量 2454

猜你喜欢

转载自blog.csdn.net/piduocheng0577/article/details/105031337