python - TinyMCE 与 Django : "This field is required"

标签 python django django-admin tinymce django-tinymce

我目前在 Django 管理界面中使用 TinyMCE 编辑器时遇到问题。当在两个特定的 TinyMCE 字段中输入文本并按“保存”时,返回的表单中两个字段均为空,标记为红色并标有“此字段为必填”标签:

enter image description here

这种行为很奇怪,因为我已经在不同的模型中实现了各种 TinyMCE 编辑器,并且运行良好。我应该澄清一下,我希望这两个字段都是强制性的。问题是输入的文本被丢弃,并且返回的表单的两个字段都为空。这是所有相关代码:

companycms/news/models.py

from django.db import models
from tinymce import models  as tinymce_models

class Article(models.Model):
    headline = models.CharField(max_length=200)
    content = tinymce_models.HTMLField()
    about = tinymce_models.HTMLField()
    pub_date = models.DateTimeField('date published')
    url = models.CharField(max_length=200)

companycms/news/forms.py

from django import forms
from django.db.models import get_model
from django.contrib.auth.models import User
from companycms.widgets import AdvancedEditor
from news.models import Article
from django.db import models

class ArticleModelAdminForm(forms.ModelForm):
    headline = forms.CharField(max_length=200)
    content = forms.CharField(widget=AdvancedEditor())
    about = forms.CharField(widget=AdvancedEditor())
    pub_date = models.DateTimeField('date published')
    url = forms.CharField(max_length=200)

    class Meta:
         model = Article

companycms/news/admin.py

from django.contrib import admin
from news.models import Article
from news.forms import ArticleModelAdminForm

class ArticleAdmin(admin.ModelAdmin):
    list_display = ('headline', 'pub_date',)
    form = ArticleModelAdminForm

admin.site.register(Article, ArticleAdmin)

companycms/companycms/widgets.py

from django import forms
from django.conf import settings
from django.utils.safestring import mark_safe

class AdvancedEditor(forms.Textarea):
    class Media:
        js = ('/static/tiny_mce/tiny_mce.js',)

    def __init__(self, language=None, attrs=None):
        self.language = language or settings.LANGUAGE_CODE[:2]
        self.attrs = {'class': 'advancededitor'}
        if attrs: self.attrs.update(attrs)
        super(AdvancedEditor, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        rendered = super(AdvancedEditor, self).render(name, value, attrs)
        return rendered + mark_safe(u'''
        <script type="text/javascript">
        tinyMCE.init({
            mode: "textareas",
            theme: "advanced",
            plugins: "advhr,table,emotions,media,insertdatetime,directionality",
            theme_advanced_toolbar_align: "left",
            theme_advanced_toolbar_location: "top",             
    theme_advanced_buttons1:"bold,italic,underline,strikethrough,sub,sup,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,formatselect,fontselect,fontsizeselect,forecolor",
theme_advanced_buttons2:"bullist,numlist,outdent,indent,ltr,rtl,separator,link,unlink,anchor,image,separator,table,insertdate,inserttime,advhr,emotions,media,charmap,separator,undo,redo",
            theme_advanced_buttons3_add:"forecolor,backcolor",


theme_advanced_font_sizes:"170%,10px,11px,12px,13px,14px,15px,16px,17px,18px,19px,20px,21px,22px,23px,24px,25px,26px,27px,28px,29px,30px,32px,48px",
            height: "350px",
            width: "653px"
        });
        </script>''')

检查了 JavaScript 控制台后,没有返回任何错误,并且我检查了其他管理页面,发现此错误不会出现在其他任何地方。

预先感谢您的帮助。

最佳答案

您应该在companycms/news/models.py中添加一个blank=True参数:

content = tinymce_models.HTMLField(blank=True)

关于python - TinyMCE 与 Django : "This field is required",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17747314/

相关文章:

python - 解析 Python 中的语法错误时出现意外的 EOF

python - 更改python中的字符串格式分隔符

python - ERR_ABORTED 404 - Django - 静态文件

django - 无法在 Admin Changeform 中反向匹配 url

python - 有没有办法获取猎豹模板中所有占位符的列表

python - 重新粉刷开罗 window ?

python - django 1.4 - 无法比较 offset-naive 和 offset-aware 日期时间

python - Django 的管理命令如何管理事务?

python - 使用 Parler 在 Django 管理界面中创建自定义表单

python - Django admin - 限制下拉列表中的选择