错误:无效的表单控件不可聚焦

问题描述 投票:0回答:1

我的 Django 表单遇到问题,我在浏览器控制台中收到以下错误消息:

  • name=‘content’的无效表单控件不可聚焦。
  • 无效的表单控件不可聚焦。

我不确定是什么原因导致此错误或如何解决它。我检查了我的 HTML 表单和 Django 视图,但我似乎找不到任何明显的问题。

顺便说一句,我对 CKEditor 和 Django 都很陌生,请原谅我提出任何“愚蠢”的请求 - 我只是找不到解决这个问题的方法。

这是我的 HTML 表单的片段:

创建.html:

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Post</title>
    <link rel="stylesheet" href="{% static 'django_ckeditor_5/dist/styles.css' %}">
</head>
<body>
<div class="container">
    <div class="card">
        <h1>Create Post</h1>
        <form action="" method="POST" enctype="multipart/form-data">
            <!-- Other fields, removed for simplicity reasons. -->
            <div class="input">
                {{ form.content }}
            </div>
            <button type="submit">Create Post</button>
        </form>
    </div>
</div>
<script src="{% static 'django_ckeditor_5/dist/bundle.js' %}"></script>
</body>
</html>

views.py(仅create.html所需的函数:

def createPost(request):
    if request.user.is_authenticated:
        if request.method == "POST":
            form = forms.CreatePostForm(request.POST, request.FILES)
            if form.is_valid():
                post = form.save(commit=False)
                post.author = request.user
                post.slug = slugify(post.title)
                post.save()
                messages.success(request, 'Post created successfully.')
                return redirect('home')
            else:
                messages.error(request, 'Error creating post. Please check the form.')
        else:
            form = forms.CreatePostForm()
        context = {'form': form}
        return render(request, 'create.html', context)
    else:
        raise PermissionDenied()

models.py:

import os
import uuid

from django.db import models
from django.utils.text import slugify
from django_ckeditor_5.fields import CKEditor5Field
from django.contrib.auth.models import User

def thumbnail_upload_path(instance, filename):
    _,ext = os.path.splitext(filename)
    new_filename = f"{uuid.uuid4()}{ext}"
    return os.path.join("uploads/thumbnails/", new_filename)

class Post(models.Model):
    title = models.CharField(max_length=50, null=False, blank=False)
    desc = models.TextField(max_length=255, null=False, blank=False)
    slug = models.SlugField(unique=True, blank=True)
    thumbnail = models.ImageField(upload_to=thumbnail_upload_path, null=False, blank=False)
    category = models.CharField(max_length=20, null=False, blank=False, default="Technology")
    keywords = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    content = CKEditor5Field('Text', config_name='extends')

    def save(self, *args, **kwargs):
        if not self.slug or self.title != self.slug:
            self.slug = slugify(self.title)
        super().save(*args, **kwargs)

    def __str__(self):
        return self.title

forms.py:

from django import forms
from django_ckeditor_5.widgets import CKEditor5Widget
from . import models

class CreatePostForm(forms.ModelForm):
    class Meta:
        model = models.Post
        fields = '__all__'
        widgets = {
            'content': CKEditor5Widget(config_name='extends'),
        }

我已经检查了 HTML 表单和 Django 视图,以确保正确包含 CKEditor5 小部件并且正确处理表单数据。尽管我付出了努力,错误消息仍然出现在浏览器中。

html django forms ckeditor5
1个回答
0
投票

已修复:

问题出在模型中的内容字段

代替:

content = CKEditor5Field('Text', config_name='extends')

一定是:

content = CKEditor5Field('Text', config_name='extends', null=True, blank=True)

感谢所有试图提供帮助的人。

© www.soinside.com 2019 - 2024. All rights reserved.