您好,登錄后才能下訂單哦!
Django 的富文本編輯器
想要用 首先 下載
pip install django-tinymce
創(chuàng)建應(yīng)用
python manage.py startapp task_1
創(chuàng)建模型
from django.db import models
from tinymce.models import HTMLField
class MessageInfo(models.Model):
username = models.CharField(max_length=20)
email = models.EmailField(blank=True, null=True)
subject = models.CharField(max_length=50)
info = HTMLField()
在settings中注冊應(yīng)用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#需要使用到第三方的靜態(tài)資源 必須注冊應(yīng)用
`'tinymce'`
]****
生成遷移文件:根據(jù)模型 類生成sql語句python manage.py makemigrations
執(zhí)行遷移:執(zhí)行sql語句生成數(shù)據(jù)表python manage.py migrate
tinymce配置
TINYMCE_DEFAULT_CONFIG = {
'theme': 'advanced',
'width': 600,
'height': 400,
}
配置項目URL
from django.conf.urls import url
from . import views
app_name= 'blog'
urlpatterns = [
url(r'^contactus/$', views.contactus, name='contactus'),
]
編寫視圖函數(shù)
from django.shortcuts import render
from .models import MessageInfo
def contactus(request):
if request.method == 'GET':
return render(request, 'contact.html')
elif request.method == 'POST':
x = MessageInfo()
x.username = request.POST['name']
x.email = request.POST['email']
x.subject = request.POST['subject']
x.info = request.POST['message']
x.save()
return render(request, 'index.html')
**編寫模板文件 **
<!DOCTYPE html>
<html>
<head>
<title>Black & White</title>
<!-- meta -->
<meta charset="UTF-8">
<script src="/static/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
'mode':'textareas',
'theme':'simple',
'width': '100%' ,
'height':100
});
</script>
</head>
<body>
<div>
<form action="{% url 'blog:contactus' %}" method="post">
<input type="text" name="name" placeholder="姓名" required>
<input type="email" name="email" placeholder="郵箱" required>
<input type="text" name="subject" placeholder="建議標(biāo)題" required>
<textarea name="message" rows="7" placeholder="輸入你的建議"></textarea>
<button type="submit">提交</button>``
</form>
</div>
</body>
</html>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。