为什么 home.html(用于添加新的 post)在添加新的 post 时不显示带有 ckeditor 工具栏的呈现的 django 表单?
why home.html (for adding a new post) is not showing the rendered django form with ckeditor toolbar in add new post?
我正在整合一些应用程序来制作一个更大的项目,但问题是当我尝试在 html 登录页面中插入 ckeditor 时它没有显示。出于这个原因,我试图制作单独的应用程序来查看这里发生了什么。我正在向您详细展示代码。
editing/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/'
editing/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 时,我的静态文件夹在 editor/static 中,ckeditor 文件夹也在 editor/static 目录中。
editor/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
editor/admin.py:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
当我执行 python manage.py makemigrations 和 python manage.py migrate,ckeditor在locahost:8000/admin的新post部分可以完美看到。然后我尝试制作模板。
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>
home.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 文件 editor/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 post 编辑器。它只是为了查看有什么问题以及为什么我的 html 文件中没有显示我的 ckeditor 工具栏。当我 运行 python manage.py 运行 服务器没有错误显示并且 html 页面显示 django post 和新 post 并且只有保存按钮。它无法呈现 django 的新形式 post。 css 样式被呈现是因为 django post 和新的 post 具有所需的颜色和字体。但为什么它不使用 ckeditor 工具栏呈现新的 post 表单?
我还在学习,这个问题让我疲惫不堪。提前谢谢
我终于解决了。正确代码如下:
对于 models.py:
class Post(models.Model):
title=models.CharField(max_length=255)
#body=models.CharField(max_length=2000)
description=RichTextUploadingField()
在 views.py 文件中,我的错误是我调用了 TemplateView ,它应该是 createView 并且此更改将在首页呈现表单。在 HomePageView class 中,字段应类似于 ['title'、'description'],而不是 'body'。其余部分相同,并且在迁移和运行服务器之后,表单显示 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']
无论如何……
我正在整合一些应用程序来制作一个更大的项目,但问题是当我尝试在 html 登录页面中插入 ckeditor 时它没有显示。出于这个原因,我试图制作单独的应用程序来查看这里发生了什么。我正在向您详细展示代码。
editing/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/'
editing/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 时,我的静态文件夹在 editor/static 中,ckeditor 文件夹也在 editor/static 目录中。
editor/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
editor/admin.py:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
当我执行 python manage.py makemigrations 和 python manage.py migrate,ckeditor在locahost:8000/admin的新post部分可以完美看到。然后我尝试制作模板。
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>
home.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 文件 editor/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 post 编辑器。它只是为了查看有什么问题以及为什么我的 html 文件中没有显示我的 ckeditor 工具栏。当我 运行 python manage.py 运行 服务器没有错误显示并且 html 页面显示 django post 和新 post 并且只有保存按钮。它无法呈现 django 的新形式 post。 css 样式被呈现是因为 django post 和新的 post 具有所需的颜色和字体。但为什么它不使用 ckeditor 工具栏呈现新的 post 表单?
我还在学习,这个问题让我疲惫不堪。提前谢谢
我终于解决了。正确代码如下:
对于 models.py:
class Post(models.Model):
title=models.CharField(max_length=255)
#body=models.CharField(max_length=2000)
description=RichTextUploadingField()
在 views.py 文件中,我的错误是我调用了 TemplateView ,它应该是 createView 并且此更改将在首页呈现表单。在 HomePageView class 中,字段应类似于 ['title'、'description'],而不是 'body'。其余部分相同,并且在迁移和运行服务器之后,表单显示 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']
无论如何……