引入富文本编辑器参考
创建pub_blog.js(static文件夹里面创建js文件夹)
//页面加载完再执行
window.onload = function () {
const {createEditor, createToolbar} = window.wangEditor
const editorConfig = {
placeholder: 'Type here...',
onChange(editor) {
const html = editor.getHtml()
console.log('editor content', html)
// 也可以同步到 <textarea>
}
}
const editor = createEditor({
selector: '#editor-container',
html: '<p><br></p>',
config: editorConfig,
mode: 'default', // or 'simple'
})
const toolbarConfig = {}
const toolbar = createToolbar({
editor,
selector: '#toolbar-container',
config: toolbarConfig,
mode: 'default', // or 'simple'
})
}
创建pub_blog.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>发布博客</title>
<link rel="stylesheet" type="text/css" href="{% static 'bootstrap5/bootstrap.min.css' %}">
<script type="text/javascript" src="{% static 'bootstrap5/popper.min.js' %}"></script>
<script type="text/javascript" src="{% static 'bootstrap5/bootstrap.min.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'css/base.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'wangeditor/style.css' %}">
<script type="text/javascript" src="{% static 'wangeditor/index.js' %}"></script>
<script type="text/javascript" src="{% static 'js/pub_blog.js' %}"></script>
<style type="text/css">
#editor—wrapper {
border: 1px solid #ccc;
z-index: 100; /* 按需定义 */
}
#toolbar-container {
border-bottom: 1px solid #ccc;
}
#editor-container {
height: 500px;
}
</style>
</head>
<body>
<header class="p-3 text-bg-light border-bottom mb-3">
<div class="container">
<div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start">
<a href="/" class="d-flex align-items-center mb-2 mb-lg-0 text-white text-decoration-none">
<img src="{% static 'image/logo.png' %}" height="40">
</a>
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
<li><a href="#" class="nav-link px-2 text-secondary">首页</a></li>
<li><a href="#" class="nav-link px-2 text-secondary">发布博客</a></li>
</ul>
<form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" role="search">
<input type="search" class="form-control" placeholder="Search..." aria-label="Search">
</form>
<div class="text-end">
<button type="button" class="btn btn-outline-primary me-2">登录</button>
<button type="button" class="btn btn-primary">注册</button>
</div>
</div>
</div>
</header>
<main class='container p-4 rounded bg-white'>
<h1>发布博客</h1>
<div class="mt-2">
<form>
<div class="mb-3">
<label class="form-label">标题</label>
<input type="text" name="title" placeholder="请输入标题" class="form-control">
</div>
<div class="mb-3">
<label class="form-label">分类</label>
<select class="form-select" name='category'>
<option value="1">Python</option>
<option value="2">前端</option>
<option value="3">大数据</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">内容</label>
<div id="editor—wrapper">
<div id="toolbar-container"><!-- 工具栏 --></div>
<div id="editor-container"><!-- 编辑器 --></div>
</div>
</div>
<div class="mb-3 text-end">
<button class="btn btn-primary">发布</button>
</div>
</form>
</div>
</main>
</body>
</html>
上面的代码中引入了富文本编辑器
创建发布博客的视图函数
def pub_blog(request):
return render(request,'pub_blog.html')
创建发布博客的路由
更改下博客详情页面路由以免有冲突
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path('',views.index,name='index'),
path('blog/detail/<blog_id>',views.blog_detail,name='blog_detail'),
path('blog/pub', views.pub_blog, name='pub_blog'),
]
测试访问