Python 为什么home.html(用于添加新帖子)没有在addnewpost中显示带有ckeditor工具栏的呈现django表单?
我正在集成一些应用程序来创建一个更大的项目,但问题是当我尝试在html登录页面中插入ckeditor时,它并没有显示出来。出于这个原因,我试图制作一个单独的应用程序,看看这里发生了什么。我将向您详细展示代码 编辑/settings.py:Python 为什么home.html(用于添加新帖子)没有在addnewpost中显示带有ckeditor工具栏的呈现django表单?,python,html,django,django-models,ckeditor,Python,Html,Django,Django Models,Ckeditor,我正在集成一些应用程序来创建一个更大的项目,但问题是当我尝试在html登录页面中插入ckeditor时,它并没有显示出来。出于这个原因,我试图制作一个单独的应用程序,看看这里发生了什么。我将向您详细展示代码 编辑/settings.py: .......... INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'djang
..........
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/'
编辑/url.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时,我的静态文件夹位于editor/static目录下,ckeditor文件夹也位于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
class Post(models.Model):
title=models.CharField(max_length=255)
#body=models.CharField(max_length=2000)
description=RichTextUploadingField()
编辑器/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>
以下是编辑器/views.py:
from django.views.generic import TemplateView
from .models import Post
class HomePageView(TemplateView):
model=Post
template_name='home.html'
fields=['title','body']
我只想看到一个简单的页面,显示新的html帖子编辑器,其中包含ckeditor工具栏。这只是为了看看有什么问题,以及为什么我的编辑器工具栏没有显示在我的html文件中。当我运行python manage.py runserver时,没有显示错误,html页面显示django post和new post,只有保存按钮。它无法呈现django新帖子的形式。呈现css样式是因为django post和new post具有所需的颜色和字体。但为什么不使用ckeditor工具栏呈现新的post表单呢
我还在学习,这个问题正在折磨着我。我终于解决了它。下面给出了正确的代码: 对于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
class Post(models.Model):
title=models.CharField(max_length=255)
#body=models.CharField(max_length=2000)
description=RichTextUploadingField()
在views.py文件中,我的错误是调用了TemplateView,它应该是createView,这个更改将在首页上呈现表单。在HomePageView类中,字段应该类似于['title','description',],而不是带有“body”。REST是相同的,在迁移并运行Server之后,表单将显示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']
无论如何,thanx…根据您的描述,看起来整个表单没有呈现出来。在主页视图中,表单本身没有任何内容。也许您应该尝试使用基于函数的视图来实现此目的。看到这个了吗