经过一整天的搜索和尝试,我发现在 Django 应用程序上使用 tinyMCE 的前端版本要容易得多,通过 TinyMCE 的 CDN 交付
内容使用 HTML 标签保存,并且可以使用 html 标签显示给用户。
我试图使解决方案尽可能通用,即应该移动和引用 javascript。如果前端所见即所得的人将比您使用更多,您应该将索引中的 |safe 更改为清理功能,以确保安全/防止不必要的代码黑客攻击。
作为一个渲染 TinyMCE 的前端 CDN 不需要“安装”后端,viewspy 和 urls.py 包含在内,以向我们展示为用户提供他们输入的内容的视图。
链接到 CDN
https://www.tinymce.com/download/
索引.html
{% load staticfiles %}
<!-- <link rel="stylesheet" type="text/css" href="{% static 'wys/style.css' %}" /> -->
<!DOCTYPE html>
<html>
<head>
<!-- Load TinyMCE from CDN -->
<script src="//cdn.tinymce.com/4/tinymce.js"></script>
<!-- Set preference of what should be visible on init -->
<script>tinymce.init({
selector: '#foo',
height: 200,
theme: 'modern',
plugins: [
'advlist autolink lists link image charmap print preview hr anchor pagebreak',
'searchreplace wordcount visualblocks visualchars code fullscreen',
'insertdatetime media nonbreaking save table contextmenu directionality',
'emoticons template paste textcolor colorpicker textpattern imagetools codesample toc'
],
toolbar1: 'undo redo | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
toolbar2: 'print preview media | forecolor backcolor emoticons | codesample',
image_advtab: true,
templates: [
{ title: 'Test template 1', content: 'Test 1' },
{ title: 'Test template 2', content: 'Test 2' }
],
content_css: [
'//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
'//www.tinymce.com/css/codepen.min.css'
]
});
</script>
</head>
<body>
<h1>This is the homepage</h1>
<h1>Below Are 2 Form Areas</h1>
<form method="GET">
Question: <input type="text" id="foo" name="search"><br/><br/><br/><br/><br/>
Name: <input type="text" id="bar" name="name"><br/><br/><br/><br/><br/><br/>
<input type="submit" value="Submit" />
</form><br/><br/>
<h3>Display of question</h3>
{% for question in questions %}
<p>{{ question.query|safe}}</p>
<p>{{ question.user_id|safe}}</p>
{% endfor %}
</body>
视图.py
from django.shortcuts import render
from .models import Queries
def MainHomePage(request):
questions=None
if request.GET.get('search'):
search = request.GET.get('search')
questions = Queries.objects.filter(query__icontains=search)
name = request.GET.get('name')
query = Queries.objects.create(query=search, user_id=name)
query.save()
return render(request, 'wys/index.html',{
'questions': questions,
})
应用程序中的 urls.py
from django.conf.urls import url
from . import views
app_name = 'wys'
urlpatterns = [
url(r'^', views.MainHomePage, name='index'),
# url(r'^', views.MainHomePage, name='index'),
]