python - 为什么 home.html(用于添加新帖子)在添加新帖子时没有显示带有 ckeditor 工具栏的渲染 django 表单?
问题描述
我正在集成一些应用程序来制作一个更大的项目,但问题是当我尝试在 html 登录页面中插入 ckeditor 时它没有显示。出于这个原因,我试图制作单独的应用程序是为了看看这里发生了什么。我正在向您详细展示代码。
编辑/settings.py:
..........
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'editor',
'ckeditor_uploader',
'ckeditor',
]
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR,'templates')],........
.....................
STATIC_URL = 'editor/static/'
STATIC_ROOT='editor/static/'
CKEDITOR_UPLOAD_PATH='editor/static/media/'
编辑/urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('editor.urls')),
path('ckeditor/',include('ckeditor_uploader.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在这种情况下,当我执行 python manage.py collectstatic 并且 ckeditor 文件夹也在 editor/static 目录中时,我的静态文件夹位于 editor/static 中。
编辑器/models.py:
from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField
# Create your models here.
class Post(models.Model):
title=models.CharField(max_length=255)
body=models.CharField(max_length=2000)
description=RichTextUploadingField()
def __str__(self):
return self.title
编辑器/admin.py:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
当我执行python manage.py makemigrations和python manage.py migrate时,可以在locahost:8000/admin的新帖子部分完美地看到 ckeditor。然后我尝试制作模板。
base.html:
{% load static %}
<html>
<head>
<title>Django blog</title>
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400"
rel="stylesheet">
<link href="{% static 'admin/css/base.css' %}" rel="stylesheet">
</head>
<body>
<div>
<header>
<div class="nav-left">
<h1><a>Django blog</a></h1>
</div>
</header>
{% block content %}
{% endblock content %}
</div>
</body>
</html>
主页.html:
{% extends 'base.html' %}
{% block content %}
<h1>New post</h1>
<form action="" method="post"enctype="multipart/form-data">
{{ form.media|safe }}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>
{% endblock content %}
然后我创建了一个 html 文件编辑器/urls.py:
from django.urls import path
from .views import HomePageView
urlpatterns=[
path('',HomePageView.as_view(), name='home'),
]
这里是 editor/views.py 也是:
from django.views.generic import TemplateView
from .models import Post
class HomePageView(TemplateView):
model=Post
template_name='home.html'
fields=['title','body']
我只想看到一个简单的页面,其中显示带有 ckeditor 工具栏的新 html 帖子编辑器。这只是为了查看有什么问题以及为什么我的 ckeditor 工具栏没有显示在我的 html 文件中。当我运行 python manage.py runserver 时没有显示错误,并且 html 页面显示 django 帖子和新帖子,并且只有保存按钮。它无法呈现 django 新帖子的形式。渲染 css 样式是因为 django 帖子和新帖子具有所需的颜色和字体。但为什么它不使用 ckeditor 工具栏呈现新的帖子表单?
我还在学习,这个问题正在吞噬我。提前谢谢
解决方案
我终于解决了。正确的代码如下:
对于models.py:
class Post(models.Model):
title=models.CharField(max_length=255)
#body=models.CharField(max_length=2000)
description=RichTextUploadingField()
在views.py 文件中,我的错误是我调用了应该是createView 的TemplateView,并且此更改将在首页呈现表单。在 HomePageView 类中,字段应该类似于 ['title','description'],而不是 'body'。其余部分相同,在 migrate 和 runserver 之后,表单显示 ckeditor。views.py 变为:
from django.views.generic import CreateView
from .models import Post
class HomePageView(CreateView):
model=Post
template_name='home.html'
fields=['title','description']
无论如何谢谢....
推荐阅读
- node.js - BodyParser 给出一个空对象
- android - 如何使android上的吐司出现直到用户触摸屏幕?
- hibernate - HQL查询如何返回实体?
- perl - 如何使用 IO::Socket::SSL 获取我自己的服务器 IP 地址
- ffmpeg - 使用 ffmpeg 命令在视频中添加多个元数据
- postgresql - Postgres docker实例在重启后不会保持状态
- javascript - 折线图路径中不必要的间隙
- vuejs2 - 使用 bootstrap-vue:如何将类添加到 b 分页组件中的页面按钮
- java - 如何在 Jmeter 中获取 UTC 时区
- java - 尽管我确保数组索引小于数组大小,但如何修复数组索引超出范围?